From 55d40713a65942a7f4dcb9a46bf81648f9d1fddb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com>
Date: Thu, 16 Jul 2009 17:16:55 +0200
Subject: [PATCH] Display the list of color schemes in a combo box

This is to make some room for embedding the color scheme editing back in
the options dialog.
---
 src/plugins/texteditor/fontsettingspage.cpp | 146 ++++++++++++--------
 src/plugins/texteditor/fontsettingspage.h   |   4 +-
 src/plugins/texteditor/fontsettingspage.ui  |  92 +++++++-----
 3 files changed, 153 insertions(+), 89 deletions(-)

diff --git a/src/plugins/texteditor/fontsettingspage.cpp b/src/plugins/texteditor/fontsettingspage.cpp
index 6698230b874..b6f371594e6 100644
--- a/src/plugins/texteditor/fontsettingspage.cpp
+++ b/src/plugins/texteditor/fontsettingspage.cpp
@@ -55,6 +55,61 @@
 namespace TextEditor {
 namespace Internal {
 
+struct ColorSchemeEntry
+{
+    ColorSchemeEntry(const QString &fileName,
+                     bool readOnly):
+        fileName(fileName),
+        name(ColorScheme::readNameOfScheme(fileName)),
+        readOnly(readOnly)
+    { }
+
+    QString fileName;
+    QString name;
+    bool readOnly;
+};
+
+
+class SchemeListModel : public QAbstractListModel
+{
+public:
+    SchemeListModel(QObject *parent = 0):
+        QAbstractListModel(parent)
+    {
+    }
+
+    int rowCount(const QModelIndex &parent) const
+    { return parent.isValid() ? 0 : m_colorSchemes.size(); }
+
+    QVariant data(const QModelIndex &index, int role) const
+    {
+        if (role == Qt::DisplayRole)
+            return m_colorSchemes.at(index.row()).name;
+
+        return QVariant();
+    }
+
+    void removeColorScheme(int index)
+    {
+        beginRemoveRows(QModelIndex(), index, index);
+        m_colorSchemes.removeAt(index);
+        endRemoveRows();
+    }
+
+    void setColorSchemes(const QList<ColorSchemeEntry> &colorSchemes)
+    {
+        m_colorSchemes = colorSchemes;
+        reset();
+    }
+
+    const ColorSchemeEntry &colorSchemeAt(int index) const
+    { return m_colorSchemes.at(index); }
+
+private:
+    QList<ColorSchemeEntry> m_colorSchemes;
+};
+
+
 class FontSettingsPagePrivate
 {
 public:
@@ -62,6 +117,7 @@ public:
                             const QString &name,
                             const QString &category,
                             const QString &trCategory);
+    ~FontSettingsPagePrivate();
 
 public:
     const QString m_name;
@@ -73,20 +129,12 @@ public:
     FontSettings m_value;
     FontSettings m_lastValue;
     Ui::FontSettingsPage ui;
-};
-
-struct ColorSchemeEntry
-{
-    QString fileName;
-    QString name;
-    bool readOnly;
+    SchemeListModel *m_schemeListModel;
 };
 
 } // namespace Internal
 } // namespace TextEditor
 
-Q_DECLARE_METATYPE(TextEditor::Internal::ColorSchemeEntry)
-
 using namespace TextEditor;
 using namespace TextEditor::Internal;
 
@@ -129,7 +177,8 @@ FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescrip
     m_settingsGroup(Core::Utils::settingsKey(category)),
     m_category(category),
     m_trCategory(trCategory),
-    m_descriptions(fd)
+    m_descriptions(fd),
+    m_schemeListModel(new SchemeListModel)
 {
     bool settingsFound = false;
     QSettings *settings = Core::ICore::instance()->settings();
@@ -168,6 +217,11 @@ FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescrip
     m_lastValue = m_value;
 }
 
+FontSettingsPagePrivate::~FontSettingsPagePrivate()
+{
+    delete m_schemeListModel;
+}
+
 
 // ------- FormatDescription
 FormatDescription::FormatDescription(const QString &name, const QString &trName, const QColor &color) :
@@ -282,10 +336,7 @@ QWidget *FontSettingsPage::createPage(QWidget *parent)
 {
     QWidget *w = new QWidget(parent);
     d_ptr->ui.setupUi(w);
-
-    d_ptr->ui.schemeListWidget->addItem(tr("Default"));
-    d_ptr->ui.schemeListWidget->setCurrentIndex(d_ptr->ui.schemeListWidget->model()->index(0, 0));
-    d_ptr->ui.editButton->setEnabled(true);
+    d_ptr->ui.schemeComboBox->setModel(d_ptr->m_schemeListModel);
 
     QFontDatabase db;
     const QStringList families = db.families();
@@ -296,8 +347,7 @@ QWidget *FontSettingsPage::createPage(QWidget *parent)
     d_ptr->ui.antialias->setChecked(d_ptr->m_value.antialias());
 
     connect(d_ptr->ui.familyComboBox, SIGNAL(activated(int)), this, SLOT(updatePointSizes()));
-    connect(d_ptr->ui.schemeListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
-            this, SLOT(colorSchemeSelected(QListWidgetItem*)));
+    connect(d_ptr->ui.schemeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(colorSchemeSelected(int)));
     connect(d_ptr->ui.cloneButton, SIGNAL(clicked()), this, SLOT(cloneColorScheme()));
     connect(d_ptr->ui.editButton, SIGNAL(clicked()), this, SLOT(editColorScheme()));
     connect(d_ptr->ui.deleteButton, SIGNAL(clicked()), this, SLOT(deleteColorScheme()));
@@ -332,25 +382,25 @@ void FontSettingsPage::updatePointSizes()
         d_ptr->ui.sizeComboBox->setCurrentIndex(idx);
 }
 
-void FontSettingsPage::colorSchemeSelected(QListWidgetItem *item)
+void FontSettingsPage::colorSchemeSelected(int index)
 {
     bool modifiable = false;
-    if (item) {
-        const ColorSchemeEntry entry = item->data(Qt::UserRole).value<ColorSchemeEntry>();
+    if (index != -1) {
+        const ColorSchemeEntry &entry = d_ptr->m_schemeListModel->colorSchemeAt(index);
         modifiable = !entry.readOnly;
     }
-    d_ptr->ui.cloneButton->setEnabled(item != 0);
+    d_ptr->ui.cloneButton->setEnabled(index != -1);
     d_ptr->ui.deleteButton->setEnabled(modifiable);
     d_ptr->ui.editButton->setEnabled(modifiable);
 }
 
 void FontSettingsPage::cloneColorScheme()
 {
-    QListWidgetItem *item = d_ptr->ui.schemeListWidget->currentItem();
-    if (!item)
+    int index = d_ptr->ui.schemeComboBox->currentIndex();
+    if (index == -1)
         return;
 
-    const ColorSchemeEntry entry = item->data(Qt::UserRole).value<ColorSchemeEntry>();
+    const ColorSchemeEntry &entry = d_ptr->m_schemeListModel->colorSchemeAt(index);
 
     // Load the currently selected color scheme
     if (!d_ptr->m_value.loadColorScheme(entry.fileName, d_ptr->m_descriptions))
@@ -372,11 +422,12 @@ void FontSettingsPage::cloneColorScheme()
 
 void FontSettingsPage::deleteColorScheme()
 {
-    QListWidgetItem *item = d_ptr->ui.schemeListWidget->currentItem();
-    if (!item)
+    int index = d_ptr->ui.schemeComboBox->currentIndex();
+    if (index == -1)
         return;
 
-    const ColorSchemeEntry entry = item->data(Qt::UserRole).value<ColorSchemeEntry>();
+    const ColorSchemeEntry &entry = d_ptr->m_schemeListModel->colorSchemeAt(index);
+
     if (!entry.readOnly) {
         int ret = QMessageBox::warning(d_ptr->ui.deleteButton->window(),
                                        tr("Delete Color Scheme"),
@@ -384,17 +435,18 @@ void FontSettingsPage::deleteColorScheme()
                                        QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 
         if (ret == QMessageBox::Yes && QFile::remove(entry.fileName))
-            refreshColorSchemeList();
+            d_ptr->m_schemeListModel->removeColorScheme(index);
     }
 }
 
 void FontSettingsPage::editColorScheme()
 {
-    QListWidgetItem *item = d_ptr->ui.schemeListWidget->currentItem();
-    if (!item)
+    int index = d_ptr->ui.schemeComboBox->currentIndex();
+    if (index == -1)
         return;
 
-    const ColorSchemeEntry entry = item->data(Qt::UserRole).value<ColorSchemeEntry>();
+    const ColorSchemeEntry &entry = d_ptr->m_schemeListModel->colorSchemeAt(index);
+
     if (entry.readOnly)
         return;
 
@@ -421,21 +473,9 @@ void FontSettingsPage::editColorScheme()
     }
 }
 
-void FontSettingsPage::addColorSchemeEntry(const QString &fileName, bool readOnly)
-{
-    ColorSchemeEntry entry;
-    entry.fileName = fileName;
-    entry.name = ColorScheme::readNameOfScheme(fileName);
-    entry.readOnly = readOnly;
-
-    QListWidgetItem *item = new QListWidgetItem(entry.name);
-    item->setData(Qt::UserRole, QVariant::fromValue<ColorSchemeEntry>(entry));
-    d_ptr->ui.schemeListWidget->addItem(item);
-}
-
 void FontSettingsPage::refreshColorSchemeList()
 {
-    d_ptr->ui.schemeListWidget->clear();
+    QList<ColorSchemeEntry> colorSchemes;
 
     QString resourcePath = Core::ICore::instance()->resourcePath();
     QDir styleDir(resourcePath + QLatin1String("/styles"));
@@ -443,28 +483,25 @@ void FontSettingsPage::refreshColorSchemeList()
     styleDir.setFilter(QDir::Files);
 
     int selected = 0;
-    int count = 0;
 
     foreach (const QString &file, styleDir.entryList()) {
         const QString fileName = styleDir.absoluteFilePath(file);
-        addColorSchemeEntry(fileName, true);
         if (d_ptr->m_value.colorSchemeFileName() == fileName)
-            selected = count;
-        ++count;
+            selected = colorSchemes.size();
+        colorSchemes.append(ColorSchemeEntry(fileName, true));
     }
 
     styleDir.setPath(customStylesPath());
 
     foreach (const QString &file, styleDir.entryList()) {
         const QString fileName = styleDir.absoluteFilePath(file);
-        addColorSchemeEntry(fileName, false);
         if (d_ptr->m_value.colorSchemeFileName() == fileName)
-            selected = count;
-        ++count;
+            selected = colorSchemes.size();
+        colorSchemes.append(ColorSchemeEntry(fileName, false));
     }
 
-    const QModelIndex s = d_ptr->ui.schemeListWidget->model()->index(selected, 0);
-    d_ptr->ui.schemeListWidget->setCurrentIndex(s);
+    d_ptr->m_schemeListModel->setColorSchemes(colorSchemes);
+    d_ptr->ui.schemeComboBox->setCurrentIndex(selected);
 }
 
 void FontSettingsPage::delayedChange()
@@ -477,8 +514,9 @@ void FontSettingsPage::apply()
     d_ptr->m_value.setFamily(d_ptr->ui.familyComboBox->currentText());
     d_ptr->m_value.setAntialias(d_ptr->ui.antialias->isChecked());
 
-    if (QListWidgetItem *item = d_ptr->ui.schemeListWidget->currentItem()) {
-        const ColorSchemeEntry entry = item->data(Qt::UserRole).value<ColorSchemeEntry>();
+    int index = d_ptr->ui.schemeComboBox->currentIndex();
+    if (index != -1) {
+        const ColorSchemeEntry &entry = d_ptr->m_schemeListModel->colorSchemeAt(index);
         if (entry.fileName != d_ptr->m_value.colorSchemeFileName())
             d_ptr->m_value.loadColorScheme(entry.fileName, d_ptr->m_descriptions);
     }
diff --git a/src/plugins/texteditor/fontsettingspage.h b/src/plugins/texteditor/fontsettingspage.h
index af009d75d44..5ffc34218ce 100644
--- a/src/plugins/texteditor/fontsettingspage.h
+++ b/src/plugins/texteditor/fontsettingspage.h
@@ -42,7 +42,6 @@
 #include <QtCore/QVector>
 
 QT_BEGIN_NAMESPACE
-class QListWidgetItem;
 class QWidget;
 QT_END_NAMESPACE
 
@@ -111,13 +110,12 @@ signals:
 private slots:
     void delayedChange();
     void updatePointSizes();
-    void colorSchemeSelected(QListWidgetItem *item);
+    void colorSchemeSelected(int index);
     void cloneColorScheme();
     void deleteColorScheme();
     void editColorScheme();
 
 private:
-    void addColorSchemeEntry(const QString &fileName, bool readOnly);
     void refreshColorSchemeList();
 
     Internal::FontSettingsPagePrivate *d_ptr;
diff --git a/src/plugins/texteditor/fontsettingspage.ui b/src/plugins/texteditor/fontsettingspage.ui
index d6e249bd930..2ff5268962b 100644
--- a/src/plugins/texteditor/fontsettingspage.ui
+++ b/src/plugins/texteditor/fontsettingspage.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>391</width>
-    <height>344</height>
+    <width>344</width>
+    <height>306</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
@@ -107,25 +107,66 @@
      <property name="title">
       <string>Color Scheme</string>
      </property>
-     <layout class="QGridLayout" name="gridLayout_2">
-      <item row="0" column="1">
-       <widget class="QPushButton" name="cloneButton">
-        <property name="text">
-         <string>Clone</string>
-        </property>
-       </widget>
+     <layout class="QVBoxLayout" name="verticalLayout_2">
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout">
+        <item>
+         <widget class="QComboBox" name="schemeComboBox">
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+            <horstretch>1</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="cloneButton">
+          <property name="text">
+           <string>Clone</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="deleteButton">
+          <property name="enabled">
+           <bool>false</bool>
+          </property>
+          <property name="text">
+           <string>Delete</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
       </item>
-      <item row="2" column="1">
-       <widget class="QPushButton" name="deleteButton">
-        <property name="enabled">
-         <bool>false</bool>
-        </property>
-        <property name="text">
-         <string>Delete</string>
-        </property>
-       </widget>
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout_2">
+        <item>
+         <widget class="QPushButton" name="editButton">
+          <property name="enabled">
+           <bool>false</bool>
+          </property>
+          <property name="text">
+           <string>Edit</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="horizontalSpacer">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>40</width>
+            <height>20</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+       </layout>
       </item>
-      <item row="4" column="1">
+      <item>
        <spacer name="verticalSpacer">
         <property name="orientation">
          <enum>Qt::Vertical</enum>
@@ -138,19 +179,6 @@
         </property>
        </spacer>
       </item>
-      <item row="0" column="0" rowspan="5">
-       <widget class="QListWidget" name="schemeListWidget"/>
-      </item>
-      <item row="1" column="1">
-       <widget class="QPushButton" name="editButton">
-        <property name="enabled">
-         <bool>false</bool>
-        </property>
-        <property name="text">
-         <string>Edit</string>
-        </property>
-       </widget>
-      </item>
      </layout>
     </widget>
    </item>
-- 
GitLab