QWidget * Table::createQtWidget(Proxy *proxy, UIProxy *uiproxy, QWidget *parent) { QTableWidget *tablewidget = new TableWidget(this, parent); tablewidget->setEnabled(enabled); tablewidget->setVisible(visible); tablewidget->setStyleSheet(QString::fromStdString(style)); size_t rowcount = rows.size(), columncount = 0; for(size_t i = 0; i < rowcount; i++) columncount = std::max(columncount, rows[i].size()); tablewidget->setRowCount(rowcount); tablewidget->setColumnCount(columncount); tablewidget->horizontalHeader()->setVisible(show_horizontal_header); tablewidget->verticalHeader()->setVisible(show_vertical_header); tablewidget->setShowGrid(show_grid); QStringList qtHorizontalHeader; for(size_t i = 0; i < horizontalHeader.size(); i++) qtHorizontalHeader << QString::fromStdString(horizontalHeader[i]); tablewidget->setHorizontalHeaderLabels(qtHorizontalHeader); QStringList qtVerticalHeader; for(size_t i = 0; i < verticalHeader.size(); i++) qtVerticalHeader << QString::fromStdString(verticalHeader[i]); tablewidget->setVerticalHeaderLabels(qtVerticalHeader); if(autosize_horizontal_header) tablewidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); if(autosize_vertical_header) tablewidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); if(sortable) tablewidget->setSortingEnabled(true); for(size_t row = 0; row < rowcount; row++) { for(size_t column = 0; column < rows[row].size(); column++) { TableItem &item = rows[row][column]; QTableWidgetItem *qtwitem = new QTableWidgetItem(QString::fromStdString(item.text)); if(item.editable) qtwitem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); else qtwitem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); tablewidget->setItem(row, column, qtwitem); } } tablewidget->setSelectionBehavior(selectionBehavior); tablewidget->setSelectionMode(selectionMode); QObject::connect(tablewidget, &QTableWidget::cellActivated, uiproxy, &UIProxy::onCellActivate); QObject::connect(tablewidget, &QTableWidget::cellChanged, uiproxy, &UIProxy::onCellActivate); QObject::connect(tablewidget, &QTableWidget::itemSelectionChanged, uiproxy, &UIProxy::onTableSelectionChange); setQWidget(tablewidget); setEditable(editable); setProxy(proxy); return tablewidget; }
KeyBinder::KeyBinder(QWidget * parent, const QString & helpText, const QString & defaultText, const QString & resetButtonText) : QWidget(parent) { this->defaultText = defaultText; enableSignal = false; // Two-column tab layout QHBoxLayout * pageKeysLayout = new QHBoxLayout(this); pageKeysLayout->setSpacing(0); pageKeysLayout->setContentsMargins(0, 0, 0, 0); // Table for category list QVBoxLayout * catListContainer = new QVBoxLayout(); catListContainer->setContentsMargins(10, 10, 10, 10); catList = new QListWidget(); catList->setFixedWidth(180); catList->setStyleSheet("QListWidget::item { font-size: 14px; } QListWidget:hover { border-color: #F6CB1C; } QListWidget::item:selected { background: #150A61; color: yellow; }"); catList->setFocusPolicy(Qt::NoFocus); connect(catList, SIGNAL(currentRowChanged(int)), this, SLOT(changeBindingsPage(int))); catListContainer->addWidget(catList); pageKeysLayout->addLayout(catListContainer); // Reset all binds button if (!resetButtonText.isEmpty()) { QPushButton * btnResetAll = new QPushButton(resetButtonText); catListContainer->addWidget(btnResetAll); btnResetAll->setFixedHeight(40); catListContainer->setStretch(1, 0); catListContainer->setSpacing(10); connect(btnResetAll, SIGNAL(clicked()), this, SIGNAL(resetAllBinds())); } // Container for pages of key bindings QWidget * bindingsPagesContainer = new QWidget(); QVBoxLayout * rightLayout = new QVBoxLayout(bindingsPagesContainer); // Scroll area for key bindings QScrollArea * scrollArea = new QScrollArea(); scrollArea->setContentsMargins(0, 0, 0, 0); scrollArea->setWidget(bindingsPagesContainer); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); scrollArea->setWidgetResizable(true); scrollArea->setFrameShape(QFrame::NoFrame); scrollArea->setStyleSheet("background: #130F2A;"); // Add key binding pages to bindings tab pageKeysLayout->addWidget(scrollArea); pageKeysLayout->setStretch(1, 1); // Custom help text QLabel * helpLabel = new QLabel(); helpLabel->setText(helpText); helpLabel->setStyleSheet("color: #130F2A; background: #F6CB1C; border: solid 4px #F6CB1C; border-radius: 10px; padding: auto 20px;"); helpLabel->setFixedHeight(24); rightLayout->addWidget(helpLabel, 0, Qt::AlignCenter); // Category list and bind table row heights const int rowHeight = 20; QSize catSize, headerSize; catSize.setHeight(36); headerSize.setHeight(24); // Category list header QListWidgetItem * catListHeader = new QListWidgetItem(tr("Category")); catListHeader->setSizeHint(headerSize); catListHeader->setFlags(Qt::NoItemFlags); catListHeader->setForeground(QBrush(QColor("#130F2A"))); catListHeader->setBackground(QBrush(QColor("#F6CB1C"))); catListHeader->setTextAlignment(Qt::AlignCenter); catList->addItem(catListHeader); // Populate bindingsPages = new QHBoxLayout(); bindingsPages->setContentsMargins(0, 0, 0, 0); rightLayout->addLayout(bindingsPages); QWidget * curPage = NULL; QVBoxLayout * curLayout = NULL; QTableWidget * curTable = NULL; bool bFirstPage = true; selectedBindTable = NULL; bindComboBoxCellMappings = new QHash<QObject *, QTableWidgetItem *>(); bindCellComboBoxMappings = new QHash<QTableWidgetItem *, QComboBox *>(); for (int i = 0; i < BINDS_NUMBER; i++) { if (cbinds[i].category != NULL) { // Add stretch at end of previous layout if (curLayout != NULL) curLayout->insertStretch(-1, 1); // Category list item QListWidgetItem * catItem = new QListWidgetItem(HWApplication::translate("binds (categories)", cbinds[i].category)); catItem->setSizeHint(catSize); catList->addItem(catItem); // Create new page curPage = new QWidget(); curLayout = new QVBoxLayout(curPage); curLayout->setSpacing(2); bindingsPages->addWidget(curPage); if (!bFirstPage) curPage->setVisible(false); } // Description if (cbinds[i].description != NULL) { QLabel * desc = new QLabel(HWApplication::translate("binds (descriptions)", cbinds[i].description)); curLayout->addWidget(desc, 0); QFrame * divider = new QFrame(); divider->setFrameShape(QFrame::HLine); divider->setFrameShadow(QFrame::Plain); curLayout->addWidget(divider, 0); } // New table if (cbinds[i].category != NULL || cbinds[i].description != NULL) { curTable = new QTableWidget(0, 2); curTable->verticalHeader()->setVisible(false); curTable->horizontalHeader()->setVisible(false); curTable->horizontalHeader()->setResizeMode(QHeaderView::Stretch); curTable->verticalHeader()->setDefaultSectionSize(rowHeight); curTable->setShowGrid(false); curTable->setStyleSheet("QTableWidget { border: none; } "); curTable->setSelectionBehavior(QAbstractItemView::SelectRows); curTable->setSelectionMode(QAbstractItemView::SingleSelection); curTable->setFocusPolicy(Qt::NoFocus); connect(curTable, SIGNAL(itemSelectionChanged()), this, SLOT(bindSelectionChanged())); connect(curTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(bindCellClicked(QTableWidgetItem *))); curLayout->addWidget(curTable, 0); } // Hidden combo box QComboBox * comboBox = CBBind[i] = new QComboBox(curTable); comboBox->setModel((QAbstractItemModel*)DataManager::instance().bindsModel()); comboBox->setVisible(false); comboBox->setFixedWidth(200); // Table row int row = curTable->rowCount(); QTableWidgetItem * nameCell = new QTableWidgetItem(HWApplication::translate("binds", cbinds[i].name)); nameCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); curTable->insertRow(row); curTable->setItem(row, 0, nameCell); QTableWidgetItem * bindCell = new QTableWidgetItem(comboBox->currentText()); bindCell->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); curTable->setItem(row, 1, bindCell); curTable->resizeColumnsToContents(); curTable->setFixedHeight(curTable->verticalHeader()->length() + 10); // Updates the text in the table cell connect(comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(bindChanged(const QString &))); // Map combo box and that row's cells to each other bindComboBoxCellMappings->insert(comboBox, bindCell); bindCellComboBoxMappings->insert(nameCell, comboBox); bindCellComboBoxMappings->insert(bindCell, comboBox); }