Commit 60905c48 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer

Embedded the color scheme editor in the options dialog again

With the new representation it fits fine, and this is a bit more
convenient for the user.
parent 68c920d1
......@@ -128,7 +128,7 @@ void ColorScheme::clear()
m_formats.clear();
}
bool ColorScheme::save(const QString &fileName)
bool ColorScheme::save(const QString &fileName) const
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
......
......@@ -99,7 +99,7 @@ public:
void clear();
bool save(const QString &fileName);
bool save(const QString &fileName) const;
bool load(const QString &fileName);
inline bool equals(const ColorScheme &cs) const
......
......@@ -27,8 +27,8 @@
**
**************************************************************************/
#include "editcolorschemedialog.h"
#include "ui_editcolorschemedialog.h"
#include "colorschemeedit.h"
#include "ui_colorschemeedit.h"
#include "texteditorconstants.h"
......@@ -54,42 +54,64 @@ namespace Internal {
class FormatsModel : public QAbstractListModel
{
public:
FormatsModel(const FormatDescriptions &fd,
const FontSettings &fontSettings,
const ColorScheme &scheme,
QObject *parent = 0):
FormatsModel(QObject *parent = 0):
QAbstractListModel(parent),
m_fd(fd),
m_scheme(scheme),
m_baseFont(fontSettings.family(), fontSettings.fontSize())
m_descriptions(0),
m_scheme(0)
{
}
void setFormatDescriptions(const FormatDescriptions *descriptions)
{
m_descriptions = descriptions;
reset();
}
void setBaseFont(const QFont &font)
{
emit layoutAboutToBeChanged(); // So the view adjust to new item height
m_baseFont = font;
emit layoutChanged();
emitDataChanged(index(0));
}
void setColorScheme(const ColorScheme *scheme)
{
m_scheme = scheme;
emitDataChanged(index(0));
}
int rowCount(const QModelIndex &parent) const
{ return parent.isValid() ? 0 : m_fd.size(); }
{
return (parent.isValid() || !m_descriptions) ? 0 : m_descriptions->size();
}
QVariant data(const QModelIndex &index, int role) const
{
const FormatDescription &description = m_fd.at(index.row());
if (!m_descriptions || !m_scheme)
return QVariant();
const FormatDescription &description = m_descriptions->at(index.row());
switch (role) {
case Qt::DisplayRole:
return description.trName();
case Qt::ForegroundRole: {
QColor foreground = m_scheme.formatFor(description.name()).foreground();
QColor foreground = m_scheme->formatFor(description.name()).foreground();
if (foreground.isValid())
return foreground;
else
return m_scheme.formatFor(QLatin1String(TextEditor::Constants::C_TEXT)).foreground();
return m_scheme->formatFor(QLatin1String(TextEditor::Constants::C_TEXT)).foreground();
}
case Qt::BackgroundRole: {
QColor background = m_scheme.formatFor(description.name()).background();
QColor background = m_scheme->formatFor(description.name()).background();
if (background.isValid())
return background;
}
case Qt::FontRole: {
QFont font = m_baseFont;
font.setBold(m_scheme.formatFor(description.name()).bold());
font.setItalic(m_scheme.formatFor(description.name()).italic());
font.setBold(m_scheme->formatFor(description.name()).bold());
font.setItalic(m_scheme->formatFor(description.name()).italic());
return font;
}
}
......@@ -98,69 +120,98 @@ public:
void emitDataChanged(const QModelIndex &i)
{
if (!m_descriptions)
return;
// If the text category changes, all indexes might have changed
if (i.row() == 0)
emit dataChanged(i, index(m_fd.size() - 1));
emit dataChanged(i, index(m_descriptions->size() - 1));
else
emit dataChanged(i, i);
}
private:
const FormatDescriptions &m_fd;
const ColorScheme &m_scheme;
const QFont m_baseFont;
const FormatDescriptions *m_descriptions;
const ColorScheme *m_scheme;
QFont m_baseFont;
};
} // namespace Internal
} // namespace TextEditor
EditColorSchemeDialog::EditColorSchemeDialog(const FormatDescriptions &fd,
const FontSettings &fontSettings,
QWidget *parent) :
QDialog(parent),
m_descriptions(fd),
m_fontSettings(fontSettings),
m_scheme(fontSettings.colorScheme()),
ColorSchemeEdit::ColorSchemeEdit(QWidget *parent) :
QWidget(parent),
m_curItem(-1),
m_ui(new Ui::EditColorSchemeDialog),
m_formatsModel(new FormatsModel(m_descriptions, m_fontSettings, m_scheme, this))
m_ui(new Ui::ColorSchemeEdit),
m_formatsModel(new FormatsModel(this))
{
m_ui->setupUi(this);
m_ui->nameEdit->setText(m_scheme.name());
m_ui->itemList->setModel(m_formatsModel);
connect(m_ui->itemList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
SLOT(itemChanged(QModelIndex)));
SLOT(currentItemChanged(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()));
connect(m_ui->boldCheckBox, SIGNAL(toggled(bool)), SLOT(checkCheckBoxes()));
connect(m_ui->italicCheckBox, SIGNAL(toggled(bool)), SLOT(checkCheckBoxes()));
}
ColorSchemeEdit::~ColorSchemeEdit()
{
delete m_ui;
}
void ColorSchemeEdit::setFormatDescriptions(const FormatDescriptions &descriptions)
{
m_descriptions = descriptions;
m_formatsModel->setFormatDescriptions(&m_descriptions);
if (!m_descriptions.empty())
m_ui->itemList->setCurrentIndex(m_formatsModel->index(0));
}
setItemListBackground(m_scheme.formatFor(QLatin1String(TextEditor::Constants::C_TEXT)).background());
void ColorSchemeEdit::setBaseFont(const QFont &font)
{
m_formatsModel->setBaseFont(font);
}
EditColorSchemeDialog::~EditColorSchemeDialog()
void ColorSchemeEdit::setReadOnly(bool readOnly)
{
delete m_ui;
const bool enabled = !readOnly;
m_ui->foregroundLabel->setEnabled(enabled);
m_ui->foregroundToolButton->setEnabled(enabled);
m_ui->backgroundLabel->setEnabled(enabled);
m_ui->backgroundToolButton->setEnabled(enabled);
m_ui->eraseBackgroundToolButton->setEnabled(enabled);
m_ui->boldCheckBox->setEnabled(enabled);
m_ui->italicCheckBox->setEnabled(enabled);
}
void EditColorSchemeDialog::accept()
void ColorSchemeEdit::setColorScheme(const ColorScheme &colorScheme)
{
m_scheme.setName(m_ui->nameEdit->text());
QDialog::accept();
m_scheme = colorScheme;
m_formatsModel->setColorScheme(&m_scheme);
setItemListBackground(m_scheme.formatFor(QLatin1String(TextEditor::Constants::C_TEXT)).background());
updateControls();
}
void EditColorSchemeDialog::itemChanged(const QModelIndex &index)
const ColorScheme &ColorSchemeEdit::colorScheme() const
{
return m_scheme;
}
void ColorSchemeEdit::currentItemChanged(const QModelIndex &index)
{
if (!index.isValid())
return;
m_curItem = index.row();
updateControls();
}
void ColorSchemeEdit::updateControls()
{
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()));
......@@ -175,7 +226,7 @@ void EditColorSchemeDialog::itemChanged(const QModelIndex &index)
m_ui->italicCheckBox->blockSignals(italicBlocked);
}
void EditColorSchemeDialog::changeForeColor()
void ColorSchemeEdit::changeForeColor()
{
if (m_curItem == -1)
return;
......@@ -194,7 +245,7 @@ void EditColorSchemeDialog::changeForeColor()
}
}
void EditColorSchemeDialog::changeBackColor()
void ColorSchemeEdit::changeBackColor()
{
if (m_curItem == -1)
return;
......@@ -215,7 +266,7 @@ void EditColorSchemeDialog::changeBackColor()
}
}
void EditColorSchemeDialog::eraseBackColor()
void ColorSchemeEdit::eraseBackColor()
{
if (m_curItem == -1)
return;
......@@ -230,7 +281,7 @@ void EditColorSchemeDialog::eraseBackColor()
}
}
void EditColorSchemeDialog::checkCheckBoxes()
void ColorSchemeEdit::checkCheckBoxes()
{
if (m_curItem == -1)
return;
......@@ -243,7 +294,7 @@ void EditColorSchemeDialog::checkCheckBoxes()
}
}
void EditColorSchemeDialog::setItemListBackground(const QColor &color)
void ColorSchemeEdit::setItemListBackground(const QColor &color)
{
QPalette pal = m_ui->itemList->palette();
pal.setColor(QPalette::Base, color);
......
......@@ -27,8 +27,8 @@
**
**************************************************************************/
#ifndef EDITCOLORSCHEMEDIALOG_H
#define EDITCOLORSCHEMEDIALOG_H
#ifndef COLORSCHEMEEDIT_H
#define COLORSCHEMEEDIT_H
#include "colorscheme.h"
#include "fontsettingspage.h"
......@@ -43,43 +43,44 @@ namespace TextEditor {
namespace Internal {
namespace Ui {
class EditColorSchemeDialog;
class ColorSchemeEdit;
}
class FormatsModel;
class EditColorSchemeDialog : public QDialog
/*!
A widget for editing a color scheme. Used in the FontSettingsPage.
*/
class ColorSchemeEdit : public QWidget
{
Q_OBJECT
public:
EditColorSchemeDialog(const FormatDescriptions &fd,
const FontSettings &fontSettings,
QWidget *parent = 0);
~EditColorSchemeDialog();
ColorSchemeEdit(QWidget *parent = 0);
~ColorSchemeEdit();
ColorScheme colorScheme() const
{ return m_scheme; }
void setFormatDescriptions(const FormatDescriptions &descriptions);
void setBaseFont(const QFont &font);
void setReadOnly(bool readOnly);
void accept();
void setColorScheme(const ColorScheme &colorScheme);
const ColorScheme &colorScheme() const;
private slots:
void itemChanged(const QModelIndex &index);
void currentItemChanged(const QModelIndex &index);
void changeForeColor();
void changeBackColor();
void eraseBackColor();
void checkCheckBoxes();
private:
void updateControls();
void setItemListBackground(const QColor &color);
const TextEditor::FormatDescriptions m_descriptions;
const FontSettings m_fontSettings;
TextEditor::FormatDescriptions m_descriptions;
ColorScheme m_scheme;
int m_curItem;
Ui::EditColorSchemeDialog *m_ui;
Ui::ColorSchemeEdit *m_ui;
FormatsModel *m_formatsModel;
};
......@@ -87,4 +88,4 @@ private:
} // namespace Internal
} // namespace TextEditor
#endif // EDITCOLORSCHEMEDIALOG_H
#endif // COLORSCHEMEEDIT_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TextEditor::Internal::ColorSchemeEdit</class>
<widget class="QWidget" name="TextEditor::Internal::ColorSchemeEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>435</width>
<height>210</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QListView" name="itemList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="_2">
<item row="2" column="0">
<widget class="QCheckBox" name="boldCheckBox">
<property name="text">
<string>Bold</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="italicCheckBox">
<property name="text">
<string>Italic</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="foregroundToolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="backgroundLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Background:</string>
</property>
<property name="buddy">
<cstring>backgroundToolButton</cstring>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="foregroundLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Foreground:</string>
</property>
<property name="buddy">
<cstring>foregroundToolButton</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="backgroundToolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="eraseBackgroundToolButton">
<property name="toolTip">
<string>Erase background</string>
</property>
<property name="text">
<string>x</string>
</property>
<property name="arrowType">
<enum>Qt::LeftArrow</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>itemList</tabstop>
<tabstop>foregroundToolButton</tabstop>
<tabstop>backgroundToolButton</tabstop>
<tabstop>eraseBackgroundToolButton</tabstop>
<tabstop>boldCheckBox</tabstop>
<tabstop>italicCheckBox</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TextEditor::Internal::EditColorSchemeDialog</class>
<widget class="QDialog" name="TextEditor::Internal::EditColorSchemeDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>461</width>
<height>352</height>
</rect>
</property>
<property name="windowTitle">
<string>Edit Color Scheme</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Name</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="nameEdit"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Text Categories</string>
</property>
<layout class="QHBoxLayout">
<item>
<widget class="QListView" name="itemList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="_2">
<item row="2" column="0">
<widget class="QCheckBox" name="boldCheckBox">
<property name="text">
<string>Bold</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="italicCheckBox">
<property name="text">
<string>Italic</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="foregroundToolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Background:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Foreground:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="backgroundToolButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="eraseBackgroundToolButton">
<property name="toolTip">
<string>Erase background</string>
</property>
<property name="text">
<string>x</string>
</property>
<property name="arrowType">
<enum>Qt::LeftArrow</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>