Allow to restore default UI colors in prefs

Patch by Daniel Ramöller (racoon), with slight modifications of mine.

Fixes: #10062
This commit is contained in:
Juergen Spitzmueller 2018-12-22 18:44:58 +01:00
parent 917ac911be
commit 81e4f8dfb6
3 changed files with 188 additions and 46 deletions

View File

@ -1149,6 +1149,10 @@ PrefColors::PrefColors(GuiPreferences * form)
connect(colorChangePB, SIGNAL(clicked()), connect(colorChangePB, SIGNAL(clicked()),
this, SLOT(changeColor())); this, SLOT(changeColor()));
connect(colorResetPB, SIGNAL(clicked()),
this, SLOT(resetColor()));
connect(colorResetAllPB, SIGNAL(clicked()),
this, SLOT(resetAllColor()));
connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()), connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()),
this, SLOT(changeLyxObjectsSelection())); this, SLOT(changeLyxObjectsSelection()));
connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)), connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)),
@ -1176,15 +1180,17 @@ void PrefColors::applyRC(LyXRC & rc) const
void PrefColors::updateRC(LyXRC const & rc) void PrefColors::updateRC(LyXRC const & rc)
{ {
for (unsigned int i = 0; i < lcolors_.size(); ++i) { for (size_type i = 0; i < lcolors_.size(); ++i) {
QColor color = QColor(guiApp->colorCache().get(lcolors_[i], false)); QColor color = QColor(guiApp->colorCache().get(lcolors_[i], false));
QPixmap coloritem(32, 32); QPixmap coloritem(32, 32);
coloritem.fill(color); coloritem.fill(color);
lyxObjectsLW->item(i)->setIcon(QIcon(coloritem)); lyxObjectsLW->item(int(i))->setIcon(QIcon(coloritem));
newcolors_[i] = curcolors_[i] = color.name(); newcolors_[i] = curcolors_[i] = color.name();
} }
syscolorsCB->setChecked(rc.use_system_colors); syscolorsCB->setChecked(rc.use_system_colors);
changeLyxObjectsSelection(); changeLyxObjectsSelection();
setDisabledResets();
} }
@ -1196,25 +1202,115 @@ void PrefColors::changeColor()
if (row < 0) if (row < 0)
return; return;
QString const color = newcolors_[row]; QString const color = newcolors_[size_t(row)];
QColor c = QColorDialog::getColor(QColor(color), qApp->focusWidget()); QColor const c = QColorDialog::getColor(QColor(color), qApp->focusWidget());
if (c.isValid() && c.name() != color) { if (setColor(row, c, color)) {
newcolors_[row] = c.name(); setDisabledResets();
QPixmap coloritem(32, 32);
coloritem.fill(c);
lyxObjectsLW->currentItem()->setIcon(QIcon(coloritem));
// emit signal // emit signal
changed(); changed();
} }
} }
void PrefColors::resetColor()
{
int const row = lyxObjectsLW->currentRow();
// just to be sure
if (row < 0)
return;
QString const color = newcolors_[size_t(row)];
QColor const c = getDefaultColorByRow(row);
if (setColor(row, c, color)) {
setDisabledResets();
// emit signal
changed();
}
}
void PrefColors::resetAllColor()
{
bool isChanged = false;
colorResetAllPB->setDisabled(true);
for (int irow = 0, count = lyxObjectsLW->count(); irow < count; ++irow) {
QString const color = newcolors_[size_t(irow)];
QColor const c = getDefaultColorByRow(irow);
if (setColor(irow, c, color))
isChanged = true;
}
if (isChanged) {
setDisabledResets();
// emit signal
changed();
}
}
bool PrefColors::setColor(int const row, QColor const new_color,
QString const old_color)
{
if (new_color.isValid() && new_color.name() != old_color) {
newcolors_[size_t(row)] = new_color.name();
QPixmap coloritem(32, 32);
coloritem.fill(new_color);
lyxObjectsLW->item(row)->setIcon(QIcon(coloritem));
return true;
}
return false;
}
void PrefColors::setDisabledResets()
{
int const row = lyxObjectsLW->currentRow();
// set disable reset buttons ...
if (row >= 0)
colorResetPB->setDisabled(isDefaultColor(row, newcolors_[size_t(row)]));
colorResetAllPB->setDisabled(true);
// ... in between process qt events to give quicker visual feedback to the user ...
guiApp->processEvents();
// ... set disable Reset All button
for (int irow = 0, count = lyxObjectsLW->count(); irow < count; ++irow) {
if (!isDefaultColor(irow, newcolors_[size_t(irow)])) {
colorResetAllPB->setDisabled(false);
// the break condition might hide performance issues
// if a non-default color is at the top of the list
break;
}
}
}
bool PrefColors::isDefaultColor(int const row, QString const color)
{
return color == getDefaultColorByRow(row).name();
}
QColor PrefColors::getDefaultColorByRow(int const row)
{
ColorSet const defaultcolor;
return defaultcolor.getX11Name(lcolors_[size_t(row)]).c_str();
}
void PrefColors::changeSysColor() void PrefColors::changeSysColor()
{ {
for (int row = 0 ; row < lyxObjectsLW->count() ; ++row) { for (int row = 0 ; row < lyxObjectsLW->count() ; ++row) {
// skip colors that are taken from system palette // skip colors that are taken from system palette
bool const disable = syscolorsCB->isChecked() bool const disable = syscolorsCB->isChecked()
&& guiApp->colorCache().isSystem(lcolors_[row]); && guiApp->colorCache().isSystem(lcolors_[size_t(row)]);
QListWidgetItem * const item = lyxObjectsLW->item(row); QListWidgetItem * const item = lyxObjectsLW->item(row);
Qt::ItemFlags const flags = item->flags(); Qt::ItemFlags const flags = item->flags();
@ -1226,9 +1322,17 @@ void PrefColors::changeSysColor()
} }
} }
void PrefColors::changeLyxObjectsSelection() void PrefColors::changeLyxObjectsSelection()
{ {
colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0); int currentRow = lyxObjectsLW->currentRow();
colorChangePB->setDisabled(currentRow < 0);
if (currentRow < 0)
colorResetPB->setDisabled(true);
else
colorResetPB->setDisabled(
isDefaultColor(currentRow, newcolors_[size_t(currentRow)]));
} }

View File

@ -255,12 +255,23 @@ public:
private Q_SLOTS: private Q_SLOTS:
void changeColor(); void changeColor();
void resetColor();
void resetAllColor();
void changeSysColor(); void changeSysColor();
void changeLyxObjectsSelection(); void changeLyxObjectsSelection();
bool setColor(int const row, QColor const new_color,
QString const old_color);
bool isDefaultColor(int const row, QString const color);
void setDisabledResets();
private: private:
///
QColor getDefaultColorByRow(int const row);
///
std::vector<ColorCode> lcolors_; std::vector<ColorCode> lcolors_;
///
std::vector<QString> curcolors_; std::vector<QString> curcolors_;
///
std::vector<QString> newcolors_; std::vector<QString> newcolors_;
}; };

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>PrefColorsUi</class> <class>PrefColorsUi</class>
<widget class="QWidget" name="PrefColorsUi"> <widget class="QWidget" name="PrefColorsUi">
@ -5,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>339</width> <width>370</width>
<height>254</height> <height>254</height>
</rect> </rect>
</property> </property>
@ -19,9 +20,45 @@
<string/> <string/>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="syscolorsCB">
<property name="toolTip">
<string>Use the color scheme of your Operating System/Desktop Environment</string>
</property>
<property name="text">
<string>&amp;Use system colors</string>
</property>
</widget>
</item>
<item row="1" column="1">
<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>
<item row="0" column="0" colspan="2"> <item row="0" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" rowspan="2"> <item row="0" column="0" rowspan="5">
<widget class="QListWidget" name="lyxObjectsLW"> <widget class="QListWidget" name="lyxObjectsLW">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Expanding"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="Expanding">
@ -42,12 +79,35 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="toolTip">
<string>Change the selected color</string>
</property>
<property name="text"> <property name="text">
<string>A&amp;lter...</string> <string>A&amp;lter...</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QPushButton" name="colorResetPB">
<property name="toolTip">
<string>Reset the selected color to its original value</string>
</property>
<property name="text">
<string>Restore &amp;Default</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QPushButton" name="colorResetAllPB">
<property name="toolTip">
<string>Reset all colors to their original value</string>
</property>
<property name="text">
<string>Restore A&amp;ll</string>
</property>
</widget>
</item>
<item row="3" column="1">
<spacer> <spacer>
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -62,39 +122,6 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="0" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="syscolorsCB">
<property name="text">
<string>&amp;Use system colors</string>
</property>
</widget>
</item>
<item row="1" column="1">
<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> </layout>
</widget> </widget>
<includes> <includes>