/* * Copyright (c) 2012 Maciej Niedzielski * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ /* * Copyright (c) 2014 Kevin Smith and Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include #include #include #include #include #include #include namespace Swift { QtHighlightEditor::QtHighlightEditor(QtSettingsProvider* settings, QWidget* parent) : QWidget(parent), settings_(settings), previousRow_(-1) { ui_.setupUi(this); //itemModel_ = new QtHighlightRulesItemModel(this); //ui_.treeView->setModel(itemModel_); //ui_.ruleWidget->setModel(itemModel_); // //for (int i = 0; i < QtHighlightRulesItemModel::NumberOfColumns; ++i) { //switch (i) { //case QtHighlightRulesItemModel::ApplyTo: //case QtHighlightRulesItemModel::Sender: //case QtHighlightRulesItemModel::Keyword: //case QtHighlightRulesItemModel::Action: //ui_.treeView->showColumn(i); //break; //default: //ui_.treeView->hideColumn(i); //break; //} //} // //setHighlightManager(NULL); // setup buttons for empty rules list // connect(ui_.listWidget, SIGNAL(currentRowChanged(int)), SLOT(onCurrentRowChanged(int))); connect(ui_.newButton, SIGNAL(clicked()), SLOT(onNewButtonClicked())); connect(ui_.deleteButton, SIGNAL(clicked()), SLOT(onDeleteButtonClicked())); connect(ui_.moveUpButton, SIGNAL(clicked()), SLOT(onMoveUpButtonClicked())); connect(ui_.moveDownButton, SIGNAL(clicked()), SLOT(onMoveDownButtonClicked())); connect(ui_.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), SLOT(onApplyButtonClick())); connect(ui_.buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), SLOT(onCancelButtonClick())); connect(ui_.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), SLOT(onOkButtonClick())); connect(ui_.radioButton, SIGNAL(clicked()), SLOT(colorOtherSelect())); connect(ui_.radioButton_2, SIGNAL(clicked()), SLOT(colorOtherSelect())); connect(ui_.radioButton_7, SIGNAL(clicked()), SLOT(colorCustomSelect())); connect(ui_.radioButton_4, SIGNAL(clicked()), SLOT(soundOtherSelect())); connect(ui_.radioButton_5, SIGNAL(clicked()), SLOT(soundOtherSelect())); connect(ui_.radioButton_6, SIGNAL(clicked()), SLOT(soundCustomSelect())); ui_.lineEdit->setVisible(false); jid_ = new QtSuggestingJIDInput(this, settings); ui_.horizontalLayout_2->addWidget(jid_); setWindowTitle(tr("Highlight Rules")); } QtHighlightEditor::~QtHighlightEditor() { } std::string formatNaturalDescription(const HighlightRule& rule) { //This rule will match messages either in chats or in rooms if the sender // is called 'Admin', and the message contains 'Urgent' (not case sensitive) // as a part of any word. std::string text = "This rule will match messages "; if (rule.getMatchChat() && rule.getMatchMUC()) { text += "either in chats or in rooms"; } else if (rule.getMatchChat()) { text += "in chats only"; } else if (rule.getMatchMUC()) { text += "in rooms only"; } else { return "This rule has not been marked as a chat or room rule!"; } std::vector senders = rule.getSenders(); std::vector keywords = rule.getKeywords(); if (!senders.empty() || !keywords.empty()) { text += " if"; } if (!senders.empty()) { text += " the sender is called '" + senders[0] + "'"; } if (!senders.empty() && !keywords.empty()) { text += ", and"; } if (!keywords.empty()) { text += " the message contains '" + keywords[0] + "'"; if (rule.getMatchCase()) { text += " (case sensivitive)"; } else { text += " (not case sensivitive)"; } if (rule.getMatchWholeWords()) { text += " as a whole word."; } else { text += " as a part of any word."; } } return text; } void QtHighlightEditor::show() { ui_.listWidget->clear(); highlightManager_->loadSettings(); foreach(const HighlightRule &rule, highlightManager_->getRules()) { QListWidgetItem *item = new QListWidgetItem(); item->setText(P2QSTRING(formatNaturalDescription(rule))); ui_.listWidget->addItem(item); } if (ui_.listWidget->count()) { selectRow(0); } QWidget::show(); QWidget::activateWindow(); } void QtHighlightEditor::setHighlightManager(HighlightManager* highlightManager) { highlightManager_ = highlightManager; //itemModel_->setHighlightManager(highlightManager); //ui_.newButton->setEnabled(highlightManager != NULL); // //ui_.ruleWidget->setEnabled(false); //ui_.deleteButton->setEnabled(false); //ui_.moveUpButton->setEnabled(false); //ui_.moveDownButton->setEnabled(false); } void QtHighlightEditor::colorOtherSelect() { ui_.foreground_2->setEnabled(false); ui_.background->setEnabled(false); } void QtHighlightEditor::colorCustomSelect() { ui_.foreground_2->setEnabled(true); ui_.background->setEnabled(true); } void QtHighlightEditor::soundOtherSelect() { ui_.soundFile->setEnabled(false); ui_.soundFileButton->setEnabled(false); } void QtHighlightEditor::soundCustomSelect() { ui_.soundFile->setEnabled(true); ui_.soundFileButton->setEnabled(true); } void QtHighlightEditor::onNewButtonClicked() { int row = getSelectedRow() + 1; highlightManager_->insertRule(row, HighlightRule()); QListWidgetItem *item = new QListWidgetItem(); item->setText("New rule"); ui_.listWidget->insertItem(row, item); selectRow(row); } void QtHighlightEditor::onDeleteButtonClicked() { int selectedRow = getSelectedRow(); delete ui_.listWidget->takeItem(selectedRow); highlightManager_->removeRule(selectedRow); } void QtHighlightEditor::onMoveUpButtonClicked() { //int row = getSelectedRow(); //assert(row > 0); // //ui_.ruleWidget->save(); //ui_.ruleWidget->setActiveIndex(QModelIndex()); //itemModel_->swapRows(row, row - 1); //selectRow(row - 1); } void QtHighlightEditor::onMoveDownButtonClicked() { //int row = getSelectedRow(); //assert(row < itemModel_->rowCount(QModelIndex()) - 1); // //ui_.ruleWidget->save(); //ui_.ruleWidget->setActiveIndex(QModelIndex()); //if (itemModel_->swapRows(row, row + 1)) { //selectRow(row + 1); //} } void QtHighlightEditor::onCurrentRowChanged(int currentRow) { //ui_.ruleWidget->save(); //ui_.ruleWidget->setActiveIndex(index); // //ui_.ruleWidget->setEnabled(index.isValid()); ui_.deleteButton->setEnabled(currentRow != -1); ui_.moveUpButton->setEnabled(currentRow != -1 && currentRow != 0); ui_.moveDownButton->setEnabled(currentRow != -1 && currentRow != (ui_.listWidget->count()-1)); if (previousRow_ != -1) { if (ui_.listWidget->count() > previousRow_) { highlightManager_->setRule(previousRow_, ruleFromDialog()); } } if (currentRow != -1) { ruleToDialog(highlightManager_->getRule(currentRow)); } else { /* grey/disable entire dialog */ } previousRow_ = currentRow; } void QtHighlightEditor::onApplyButtonClick() { highlightManager_->storeSettings(); } void QtHighlightEditor::onCancelButtonClick() { close(); } void QtHighlightEditor::onOkButtonClick() { highlightManager_->storeSettings(); close(); } void QtHighlightEditor::selectRow(int row) { for (int i = 0; i < ui_.listWidget->count(); ++i) { if (i == row) { ui_.listWidget->item(i)->setSelected(true); onCurrentRowChanged(i); } } } /** Return index of selected row or -1 if none is selected */ int QtHighlightEditor::getSelectedRow() const { for (int i = 0; i < ui_.listWidget->count(); ++i) { if (ui_.listWidget->item(i)->isSelected()) { return i; } } return -1; } HighlightRule QtHighlightEditor::ruleFromDialog() { HighlightRule rule; rule.setMatchChat(ui_.checkBox->isChecked()); rule.setMatchMUC(ui_.checkBox_2->isChecked()); if (ui_.checkBox_3->isChecked()) { QString senderName = jid_->text(); if (!senderName.isEmpty()) { std::vector senders; senders.push_back(Q2PSTRING(senderName)); rule.setSenders(senders); } } if (ui_.checkBox_4->isChecked()) { QString keywordString = ui_.lineEdit_2->text(); if (!keywordString.isEmpty()) { std::vector keywords; keywords.push_back(Q2PSTRING(keywordString)); rule.setKeywords(keywords); } } rule.setNickIsKeyword(ui_.nickIsKeyword->isChecked()); rule.setMatchWholeWords(ui_.matchWholeWords_2->isChecked()); rule.setMatchCase(ui_.matchCase_2->isChecked()); HighlightAction& action = rule.getAction(); if (ui_.radioButton->isChecked()) { action.setHighlightText(false); } else if (ui_.radioButton_2->isChecked()) { action.setHighlightText(true); action.setTextColor(""); action.setTextBackground(""); } else { action.setHighlightText(true); action.setTextColor("#ffffffff"); action.setTextBackground("#ffffffff"); } return rule; } void QtHighlightEditor::ruleToDialog(const HighlightRule& rule) { ui_.checkBox->setChecked(rule.getMatchChat()); ui_.checkBox_2->setChecked(rule.getMatchMUC()); std::vector senders = rule.getSenders(); if (senders.empty()) { ui_.checkBox_3->setChecked(false); jid_->setEnabled(false); jid_->setText(""); } else { ui_.checkBox_3->setChecked(true); jid_->setEnabled(true); jid_->setText(P2QSTRING(senders[0])); } std::vector keywords = rule.getKeywords(); if (keywords.empty()) { ui_.checkBox_4->setChecked(false); ui_.lineEdit_2->setEnabled(false); ui_.lineEdit_2->setText(""); } else { ui_.checkBox_4->setChecked(true); ui_.lineEdit_2->setEnabled(true); ui_.lineEdit_2->setText(P2QSTRING(keywords[0])); } ui_.nickIsKeyword->setChecked(rule.getNickIsKeyword()); ui_.matchWholeWords_2->setChecked(rule.getMatchWholeWords()); ui_.matchCase_2->setChecked(rule.getMatchCase()); const HighlightAction& action = rule.getAction(); if (action.highlightText()) { if (action.getTextColor().empty() && action.getTextBackground().empty()) { ui_.radioButton_2->setChecked(true); } else { ui_.radioButton_7->setChecked(true); } } else { ui_.radioButton->setChecked(true); } if (action.playSound()) { if (action.getSoundFile().empty()) { ui_.radioButton_5->setChecked(true); } else { ui_.radioButton_6->setChecked(true); } } else { ui_.radioButton_4->setChecked(true); } } }