diff --git a/src/plugins/texteditor/editcolorschemedialog.cpp b/src/plugins/texteditor/editcolorschemedialog.cpp index 3d724dea401ea07a3e341d1906ff7ca85f980c2a..4684db017bd49a3cd19ae3a3346e2db130d45c45 100644 --- a/src/plugins/texteditor/editcolorschemedialog.cpp +++ b/src/plugins/texteditor/editcolorschemedialog.cpp @@ -30,6 +30,7 @@ #include "editcolorschemedialog.h" #include "ui_editcolorschemedialog.h" +#include <QtCore/QAbstractListModel> #include <QtGui/QColorDialog> using namespace TextEditor; @@ -45,6 +46,31 @@ static inline QString colorButtonStyleSheet(const QColor &bgColor) return QLatin1String("border: 2px dotted black; border-radius: 2px;"); } +namespace { + +class FormatsModel : public QAbstractListModel +{ +public: + FormatsModel(const FormatDescriptions &fd): m_fd(fd) + { } + + int rowCount(const QModelIndex &parent) const + { return parent.isValid() ? 0 : m_fd.size(); } + + QVariant data(const QModelIndex &index, int role) const + { + if (role == Qt::DisplayRole) + return m_fd.at(index.row()).trName(); + + return QVariant(); + } + +private: + const FormatDescriptions &m_fd; +}; + +} // anonymous namespace + EditColorSchemeDialog::EditColorSchemeDialog(const FormatDescriptions &fd, const FontSettings &fontSettings, QWidget *parent) : @@ -59,10 +85,11 @@ EditColorSchemeDialog::EditColorSchemeDialog(const FormatDescriptions &fd, m_ui->nameEdit->setText(m_scheme.name()); - foreach (const FormatDescription &d, fd) - m_ui->itemListWidget->addItem(d.trName()); + FormatsModel *model = new FormatsModel(m_descriptions); + m_ui->itemList->setModel(model); - connect(m_ui->itemListWidget, SIGNAL(itemSelectionChanged()), SLOT(itemChanged())); + connect(m_ui->itemList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), + SLOT(itemChanged(QModelIndex))); connect(m_ui->foregroundToolButton, SIGNAL(clicked()), SLOT(changeForeColor())); connect(m_ui->backgroundToolButton, SIGNAL(clicked()), SLOT(changeBackColor())); connect(m_ui->eraseBackgroundToolButton, SIGNAL(clicked()), SLOT(eraseBackColor())); @@ -70,7 +97,7 @@ EditColorSchemeDialog::EditColorSchemeDialog(const FormatDescriptions &fd, connect(m_ui->italicCheckBox, SIGNAL(toggled(bool)), SLOT(checkCheckBoxes())); if (!m_descriptions.empty()) - m_ui->itemListWidget->setCurrentRow(0); + m_ui->itemList->setCurrentIndex(model->index(0)); } EditColorSchemeDialog::~EditColorSchemeDialog() @@ -84,32 +111,26 @@ void EditColorSchemeDialog::accept() QDialog::accept(); } -void EditColorSchemeDialog::itemChanged() +void EditColorSchemeDialog::itemChanged(const QModelIndex &index) { - QListWidgetItem *item = m_ui->itemListWidget->currentItem(); - if (!item) + if (!index.isValid()) return; - const int numFormats = m_descriptions.size(); - for (int i = 0; i < numFormats; i++) { - if (m_descriptions[i].trName() == item->text()) { - m_curItem = i; - const Format &format = m_scheme.formatFor(m_descriptions[i].name()); - m_ui->foregroundToolButton->setStyleSheet(colorButtonStyleSheet(format.foreground())); - m_ui->backgroundToolButton->setStyleSheet(colorButtonStyleSheet(format.background())); - - m_ui->eraseBackgroundToolButton->setEnabled(i > 0 && format.background().isValid()); - - const bool boldBlocked = m_ui->boldCheckBox->blockSignals(true); - m_ui->boldCheckBox->setChecked(format.bold()); - m_ui->boldCheckBox->blockSignals(boldBlocked); - const bool italicBlocked = m_ui->italicCheckBox->blockSignals(true); - m_ui->italicCheckBox->setChecked(format.italic()); - m_ui->italicCheckBox->blockSignals(italicBlocked); - updatePreview(); - break; - } - } + m_curItem = index.row(); + + const Format &format = m_scheme.formatFor(m_descriptions[m_curItem].name()); + m_ui->foregroundToolButton->setStyleSheet(colorButtonStyleSheet(format.foreground())); + m_ui->backgroundToolButton->setStyleSheet(colorButtonStyleSheet(format.background())); + + m_ui->eraseBackgroundToolButton->setEnabled(m_curItem > 0 && format.background().isValid()); + + const bool boldBlocked = m_ui->boldCheckBox->blockSignals(true); + m_ui->boldCheckBox->setChecked(format.bold()); + m_ui->boldCheckBox->blockSignals(boldBlocked); + const bool italicBlocked = m_ui->italicCheckBox->blockSignals(true); + m_ui->italicCheckBox->setChecked(format.italic()); + m_ui->italicCheckBox->blockSignals(italicBlocked); + updatePreview(); } void EditColorSchemeDialog::changeForeColor() @@ -124,11 +145,9 @@ void EditColorSchemeDialog::changeForeColor() p.setColor(QPalette::Active, QPalette::Button, newColor); m_ui->foregroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor)); - const int numFormats = m_descriptions.size(); - for (int i = 0; i < numFormats; i++) { - QList<QListWidgetItem*> items = m_ui->itemListWidget->findItems(m_descriptions[i].trName(), Qt::MatchExactly); - if (!items.isEmpty() && items.first()->isSelected()) - m_scheme.formatFor(m_descriptions[i].name()).setForeground(newColor); + foreach (const QModelIndex &index, m_ui->itemList->selectionModel()->selectedRows()) { + const QString category = m_descriptions[index.row()].name(); + m_scheme.formatFor(category).setForeground(newColor); } updatePreview(); @@ -145,11 +164,9 @@ void EditColorSchemeDialog::changeBackColor() m_ui->backgroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor)); m_ui->eraseBackgroundToolButton->setEnabled(true); - const int numFormats = m_descriptions.size(); - for (int i = 0; i < numFormats; i++) { - QList<QListWidgetItem*> items = m_ui->itemListWidget->findItems(m_descriptions[i].trName(), Qt::MatchExactly); - if (!items.isEmpty() && items.first()->isSelected()) - m_scheme.formatFor(m_descriptions[i].name()).setBackground(newColor); + foreach (const QModelIndex &index, m_ui->itemList->selectionModel()->selectedRows()) { + const QString category = m_descriptions[index.row()].name(); + m_scheme.formatFor(category).setBackground(newColor); } updatePreview(); @@ -161,14 +178,12 @@ void EditColorSchemeDialog::eraseBackColor() return; QColor newColor; m_ui->backgroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor)); + m_ui->eraseBackgroundToolButton->setEnabled(false); - const int numFormats = m_descriptions.size(); - for (int i = 0; i < numFormats; i++) { - QList<QListWidgetItem*> items = m_ui->itemListWidget->findItems(m_descriptions[i].trName(), Qt::MatchExactly); - if (!items.isEmpty() && items.first()->isSelected()) - m_scheme.formatFor(m_descriptions[i].name()).setBackground(newColor); + foreach (const QModelIndex &index, m_ui->itemList->selectionModel()->selectedRows()) { + const QString category = m_descriptions[index.row()].name(); + m_scheme.formatFor(category).setBackground(newColor); } - m_ui->eraseBackgroundToolButton->setEnabled(false); updatePreview(); } @@ -177,14 +192,13 @@ void EditColorSchemeDialog::checkCheckBoxes() { if (m_curItem == -1) return; - const int numFormats = m_descriptions.size(); - for (int i = 0; i < numFormats; i++) { - QList<QListWidgetItem*> items = m_ui->itemListWidget->findItems(m_descriptions[i].trName(), Qt::MatchExactly); - if (!items.isEmpty() && items.first()->isSelected()) { - m_scheme.formatFor(m_descriptions[i].name()).setBold(m_ui->boldCheckBox->isChecked()); - m_scheme.formatFor(m_descriptions[i].name()).setItalic(m_ui->italicCheckBox->isChecked()); - } + + foreach (const QModelIndex &index, m_ui->itemList->selectionModel()->selectedRows()) { + const QString category = m_descriptions[index.row()].name(); + m_scheme.formatFor(category).setBold(m_ui->boldCheckBox->isChecked()); + m_scheme.formatFor(category).setItalic(m_ui->italicCheckBox->isChecked()); } + updatePreview(); } diff --git a/src/plugins/texteditor/editcolorschemedialog.h b/src/plugins/texteditor/editcolorschemedialog.h index 77bc7c69dfe0ed30febc692cc4efb0fbc7a80af8..f611579751a2ed2a8876e6591893b5fe724641f1 100644 --- a/src/plugins/texteditor/editcolorschemedialog.h +++ b/src/plugins/texteditor/editcolorschemedialog.h @@ -35,6 +35,10 @@ #include <QtGui/QDialog> +QT_BEGIN_NAMESPACE +class QModelIndex; +QT_END_NAMESPACE + namespace TextEditor { namespace Internal { @@ -58,7 +62,7 @@ public: void accept(); private slots: - void itemChanged(); + void itemChanged(const QModelIndex &index); void changeForeColor(); void changeBackColor(); void eraseBackColor(); diff --git a/src/plugins/texteditor/editcolorschemedialog.ui b/src/plugins/texteditor/editcolorschemedialog.ui index ca9487ade7d7f6b45523520b0180a64d1e54423f..bb81b75a6be2ad5fb255df09121c32e0fbdea58c 100644 --- a/src/plugins/texteditor/editcolorschemedialog.ui +++ b/src/plugins/texteditor/editcolorschemedialog.ui @@ -39,7 +39,7 @@ </property> <layout class="QHBoxLayout"> <item> - <widget class="QListWidget" name="itemListWidget"> + <widget class="QListView" name="itemList"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>1</horstretch> @@ -49,6 +49,9 @@ <property name="selectionMode"> <enum>QAbstractItemView::ExtendedSelection</enum> </property> + <property name="uniformItemSizes"> + <bool>true</bool> + </property> </widget> </item> <item> @@ -199,7 +202,7 @@ </widget> <tabstops> <tabstop>nameEdit</tabstop> - <tabstop>itemListWidget</tabstop> + <tabstop>itemList</tabstop> <tabstop>foregroundToolButton</tabstop> <tabstop>backgroundToolButton</tabstop> <tabstop>eraseBackgroundToolButton</tabstop>