Commit b76eafd8 authored by David Jarvie's avatar David Jarvie
Browse files

Use KColorButton for picking colours

svn path=/trunk/KDE/kdepim/kalarm/lib/; revision=858097
parent aa27dcc1
......@@ -17,8 +17,7 @@ add_subdirectory( autostart )
set(libkalarm_SRCS
lib/buttongroup.cpp
lib/checkbox.cpp
lib/colourcombo.cpp
lib/colourlist.cpp
lib/colourbutton.cpp
lib/combobox.cpp
lib/dateedit.cpp
lib/datetime.cpp
......
......@@ -24,7 +24,7 @@
#include "buttongroup.h"
#include "checkbox.h"
#include "colourcombo.h"
#include "colourbutton.h"
#include "emailidcombo.h"
#include "fontcolourbutton.h"
#include "functions.h"
......@@ -199,7 +199,7 @@ void EditDisplayAlarmDlg::type_init(QWidget* parent, QVBoxLayout* frameLayout)
hlayout->addWidget(mBgColourBox);
hlayout->addStretch();
label = new QLabel(i18nc("@label:listbox", "Background color:"), mBgColourBox);
mBgColourButton = new ColourCombo(mBgColourBox);
mBgColourButton = new ColourButton(mBgColourBox);
label->setBuddy(mBgColourButton);
mBgColourBox->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
......
......@@ -33,9 +33,9 @@ class KComboBox;
class KHBox;
class EmailIdCombo;
class CheckBox;
class ColourCombo;
class ComboBox;
class FontColourButton;
class ColourButton;
class ButtonGroup;
class RadioButton;
class Reminder;
......@@ -93,7 +93,7 @@ class EditDisplayAlarmDlg : public EditAlarmDlg
SoundPicker* mSoundPicker;
CheckBox* mConfirmAck;
FontColourButton* mFontColourButton; // for text display option
ColourCombo* mBgColourButton; // for file display option
ColourButton* mBgColourButton; // for file display option
KHBox* mBgColourBox;
SpecialActionsButton* mSpecialActionsButton;
QHBoxLayout* mSpecialActionsLayout1; // for text display option
......
......@@ -33,18 +33,15 @@
#include "kalarmapp.h"
#include "preferences.h"
#include "colourcombo.h"
#include "colourbutton.h"
#include "checkbox.h"
#include "fontcolour.moc"
FontColourChooser::FontColourChooser(QWidget *parent,
const QStringList &fontList, const QString& frameLabel, bool editColours,
bool fg, bool defaultFont, int visibleListSize)
const QStringList &fontList, const QString& frameLabel, bool fg, bool defaultFont, int visibleListSize)
: QWidget(parent),
mFgColourButton(0),
mRemoveColourButton(0),
mColourList(Preferences::messageColours()),
mReadOnly(false)
{
QVBoxLayout* topLayout = new QVBoxLayout(this);
......@@ -74,8 +71,8 @@ FontColourChooser::FontColourChooser(QWidget *parent,
QLabel* label = new QLabel(i18nc("@label:listbox", "Foreground color:"), box);
box->setStretchFactor(new QWidget(box), 0);
mFgColourButton = new ColourCombo(box);
connect(mFgColourButton, SIGNAL(activated(const QString&)), SLOT(setSampleColour()));
mFgColourButton = new ColourButton(box);
connect(mFgColourButton, SIGNAL(changed(const QColor&)), SLOT(setSampleColour()));
label->setBuddy(mFgColourButton);
box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message foreground color"));
}
......@@ -87,30 +84,12 @@ FontColourChooser::FontColourChooser(QWidget *parent,
QLabel* label = new QLabel(i18nc("@label:listbox", "Background color:"), box);
box->setStretchFactor(new QWidget(box), 0);
mBgColourButton = new ColourCombo(box);
connect(mBgColourButton, SIGNAL(activated(const QString&)), SLOT(setSampleColour()));
mBgColourButton = new ColourButton(box);
connect(mBgColourButton, SIGNAL(changed(const QColor&)), SLOT(setSampleColour()));
label->setBuddy(mBgColourButton);
box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
hlayout->addStretch();
if (editColours)
{
QHBoxLayout* layout = new QHBoxLayout();
layout->setMargin(0);
topLayout->addLayout(layout);
QPushButton* button = new QPushButton(i18nc("@action:button", "Add Color..."), page);
button->setFixedSize(button->sizeHint());
connect(button, SIGNAL(clicked()), SLOT(slotAddColour()));
button->setWhatsThis(i18nc("@info:whatsthis", "Choose a new color to add to the color selection list."));
layout->addWidget(button);
mRemoveColourButton = new QPushButton(i18nc("@action:button", "Remove Color"), page);
mRemoveColourButton->setFixedSize(mRemoveColourButton->sizeHint());
connect(mRemoveColourButton, SIGNAL(clicked()), SLOT(slotRemoveColour()));
mRemoveColourButton->setWhatsThis(i18nc("@info:whatsthis", "Remove the color currently shown in the background color chooser, from the color selection list."));
layout->addWidget(mRemoveColourButton);
}
if (defaultFont)
{
QHBoxLayout* layout = new QHBoxLayout();
......@@ -171,8 +150,6 @@ void FontColourChooser::setSampleColour()
mFontChooser->setBackgroundColor(bg);
QColor fg = fgColour();
mFontChooser->setColor(fg);
if (mRemoveColourButton)
mRemoveColourButton->setEnabled(!mBgColourButton->isCustomColor()); // no deletion of custom colour
}
QColor FontColourChooser::bgColour() const
......@@ -247,30 +224,3 @@ void FontColourChooser::slotDefaultFontToggled(bool on)
{
mFontChooser->setEnabled(!on);
}
void FontColourChooser::setColours(const ColourList& colours)
{
mColourList = colours;
mBgColourButton->setColours(mColourList);
mFontChooser->setBackgroundColor(mBgColourButton->color());
}
void FontColourChooser::slotAddColour()
{
QColor colour;
if (KColorDialog::getColor(colour, this) == QDialog::Accepted)
{
mColourList.insert(colour);
mBgColourButton->setColours(mColourList);
}
}
void FontColourChooser::slotRemoveColour()
{
if (!mBgColourButton->isCustomColor())
{
mColourList.remove(mBgColourButton->color());
mBgColourButton->setColours(mColourList);
}
}
......@@ -24,12 +24,11 @@
#include <QWidget>
#include <QStringList>
#include <kdeversion.h>
#include "colourlist.h"
class QPushButton;
class KFontChooser;
class CheckBox;
class ColourCombo;
class ColourButton;
class FontColourChooser : public QWidget
......@@ -39,7 +38,7 @@ public:
explicit FontColourChooser(QWidget* parent = 0,
const QStringList& fontList = QStringList(),
const QString& frameLabel = i18n("Requested font"),
bool editColours = false, bool fg = true, bool defaultFont = false,
bool fg = true, bool defaultFont = false,
int visibleListSize = 8);
void setDefaultFont();
......@@ -48,10 +47,8 @@ public:
QFont font() const;
QColor fgColour() const;
QColor bgColour() const;
const ColourList& colours() const { return mColourList; }
void setFgColour(const QColor&);
void setBgColour(const QColor&);
void setColours(const ColourList&);
QString sampleText() const;
void setSampleText(const QString& text);
bool isReadOnly() const { return mReadOnly; }
......@@ -61,16 +58,12 @@ public:
private slots:
void setSampleColour();
void slotDefaultFontToggled(bool);
void slotAddColour();
void slotRemoveColour();
private:
ColourCombo* mFgColourButton; // or null
ColourCombo* mBgColourButton;
QPushButton* mRemoveColourButton;
ColourButton* mFgColourButton; // or null
ColourButton* mBgColourButton;
KFontChooser* mFontChooser;
CheckBox* mDefaultFont; // or null
ColourList mColourList;
bool mReadOnly;
};
......
......@@ -94,7 +94,7 @@ FontColourDlg::FontColourDlg(const QColor& bgColour, const QColor& fgColour, con
QVBoxLayout* layout = new QVBoxLayout(page);
layout->setMargin(0);
layout->setSpacing(spacingHint());
mChooser = new FontColourChooser(page, QStringList(), QString(), false, true, true);
mChooser = new FontColourChooser(page, QStringList(), QString(), true, true);
mChooser->setBgColour(bgColour);
mChooser->setFgColour(fgColour);
if (defaultFont)
......
......@@ -12,8 +12,6 @@
<include>kglobalsettings.h</include>
<include>"timeperiod.h"</include>
<signal name="messageColoursChanged">
</signal>
<signal name="showInSystemTrayChanged">
<argument type="Bool">ShowInSystemTray</argument>
</signal>
......@@ -59,15 +57,10 @@
<whatsthis context="@info:whatsthis">Select the holiday region to use.</whatsthis>
<emit signal="base_HolidayRegionChanged"/>
</entry>
<entry name="Base_MessageColours" key="MessageColours" type="StringList">
<label context="@label">Color selection list</label>
<whatsthis context="@info:whatsthis">Comma separated list of color codes presented as choice for alarm messages.</whatsthis>
<code>
QStringList defaultColours;
defaultColours &lt;&lt; QColor(Qt::red).name() &lt;&lt; QColor(Qt::green).name() &lt;&lt; QColor(Qt::blue).name() &lt;&lt; QColor(Qt::cyan).name() &lt;&lt; QColor(Qt::magenta).name() &lt;&lt; QColor(Qt::yellow).name() &lt;&lt; QColor(Qt::white).name() &lt;&lt; QColor(Qt::lightGray).name() &lt;&lt; QColor(Qt::black).name();
</code>
<default code="true">defaultColours</default>
<emit signal="messageColoursChanged"/>
<entry name="DefaultFgColour" key="MessageForegroundColour" type="Color">
<label context="@label">Foreground color</label>
<whatsthis context="@info:whatsthis">Default foreground color for alarm message windows.</whatsthis>
<default code="true">Qt::black</default>
</entry>
<entry name="DefaultBgColour" key="MessageBackgroundColour" type="Color">
<label context="@label">Background color</label>
......
/*
* colourcombo.cpp - colour selection combo box
* colourbutton.cpp - colour selection button
* Program: kalarm
* Copyright (c) 2001-2003,2005-2007 by David Jarvie <software@astrojar.org.uk>
* Copyright © 2008 by David Jarvie <djarvie@kde.org>
*
* Some code taken from kdelibs/kdeui/kcolorcombo.cpp in the KDE libraries:
* Copyright (C) 1997 Martin Jones (mjones@kde.org)
......@@ -24,41 +24,21 @@
#include <QMouseEvent>
#include <QKeyEvent>
#include "preferences.h"
#include "colourcombo.moc"
#include "colourbutton.h"
ColourCombo::ColourCombo(QWidget* parent, const QColor& defaultColour)
: KColorCombo(parent),
ColourButton::ColourButton(QWidget* parent)
: KColorButton(parent),
mReadOnly(false)
{
setColours(Preferences::messageColours());
setColor(defaultColour);
Preferences::connect(SIGNAL(messageColoursChanged()), this, SLOT(slotPreferencesChanged()));
}
/******************************************************************************
* Set a new colour selection.
*/
void ColourCombo::setColours(const ColourList& colours)
{
setColors(colours.qcolorList());
}
/******************************************************************************
* Called when the user changes the colour list in the preference settings.
*/
void ColourCombo::slotPreferencesChanged()
{
setColors(Preferences::messageColours().qcolorList());
}
void ColourCombo::setReadOnly(bool ro)
void ColourButton::setReadOnly(bool ro)
{
mReadOnly = ro;
}
void ColourCombo::mousePressEvent(QMouseEvent* e)
void ColourButton::mousePressEvent(QMouseEvent* e)
{
if (mReadOnly)
{
......@@ -66,29 +46,29 @@ void ColourCombo::mousePressEvent(QMouseEvent* e)
if (e->button() == Qt::LeftButton)
return;
}
KColorCombo::mousePressEvent(e);
KColorButton::mousePressEvent(e);
}
void ColourCombo::mouseReleaseEvent(QMouseEvent* e)
void ColourButton::mouseReleaseEvent(QMouseEvent* e)
{
if (!mReadOnly)
KColorCombo::mouseReleaseEvent(e);
KColorButton::mouseReleaseEvent(e);
}
void ColourCombo::mouseMoveEvent(QMouseEvent* e)
void ColourButton::mouseMoveEvent(QMouseEvent* e)
{
if (!mReadOnly)
KColorCombo::mouseMoveEvent(e);
KColorButton::mouseMoveEvent(e);
}
void ColourCombo::keyPressEvent(QKeyEvent* e)
void ColourButton::keyPressEvent(QKeyEvent* e)
{
if (!mReadOnly || e->key() == Qt::Key_Escape)
KColorCombo::keyPressEvent(e);
KColorButton::keyPressEvent(e);
}
void ColourCombo::keyReleaseEvent(QKeyEvent* e)
void ColourButton::keyReleaseEvent(QKeyEvent* e)
{
if (!mReadOnly)
KColorCombo::keyReleaseEvent(e);
KColorButton::keyReleaseEvent(e);
}
/*
* colourcombo.h - colour selection combo box
* colourbutton.h - colour selection button
* Program: kalarm
* Copyright © 2001-2003,2005-2007 by David Jarvie <software@astrojar.org.uk>
* Copyright © 2008 by David Jarvie <djarvie@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -18,41 +18,34 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef COLOURCOMBO_H
#define COLOURCOMBO_H
#ifndef COLOURBUTTON_H
#define COLOURBUTTON_H
#include <kcolorcombo.h>
#include "colourlist.h"
class QMouseEvent;
class QKeyEvent;
#include <kcolorbutton.h>
/**
* @short A colour selection combo box with read-only option.
* @short A colour selection button with read-only option.
*
* The ColourCombo class is a KColorCombo with a read-only option.
* The ColourButton class is a KColorButton with a read-only option.
*
* The widget may be set as read-only. This has the same effect as disabling it, except
* that its appearance is unchanged.
*
* @author David Jarvie <software@astrojar.org.uk>
* @author David Jarvie <djarvie@kde.org>
*/
class ColourCombo : public KColorCombo
class ColourButton : public KColorButton
{
Q_OBJECT
public:
/** Constructor.
* @param parent The parent object of this widget.
* @param defaultColour The colour which is selected by default.
*/
explicit ColourCombo(QWidget* parent = 0, const QColor& defaultColour = 0xFFFFFF);
explicit ColourButton(QWidget* parent = 0);
/** Returns the selected colour. */
QColor colour() const { return color(); }
/** Sets the selected colour to @p c. */
void setColour(const QColor& c) { setColor(c); }
/** Initialises the list of colours to @p list. */
void setColours(const ColourList& list);
/** Returns true if the widget is read only. */
bool isReadOnly() const { return mReadOnly; }
/** Sets whether the combo box can be changed by the user.
......@@ -65,11 +58,8 @@ class ColourCombo : public KColorCombo
virtual void mouseMoveEvent(QMouseEvent*);
virtual void keyPressEvent(QKeyEvent*);
virtual void keyReleaseEvent(QKeyEvent*);
private slots:
void slotPreferencesChanged();
private:
ColourList mColourList; // the sorted colours to display
bool mReadOnly; // value cannot be changed
};
#endif // COLOURCOMBO_H
#endif // COLOURBUTTON_H
/*
* colourlist.cpp - an ordered list of colours
* Program: kalarm
* Copyright © 2003,2005,2007 by David Jarvie <software@astrojar.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "colourlist.h"
ColourList::ColourList(const QColor* colours)
{
while (colours->isValid())
mList << (*colours++).rgb();
qSort(mList);
}
void ColourList::insert(const QColor& colour)
{
QRgb rgb = colour.rgb();
for (int i = 0, count = mList.count(); i < count; ++i)
{
if (rgb <= mList[i])
{
if (rgb != mList[i]) // don't insert duplicates
mList.insert(i, rgb);
return;
}
}
mList.append(rgb);
}
ColourList& ColourList::operator=(const QList<QColor>& list)
{
mList.clear();
for (int i = 0, count = list.count(); i < count; ++i)
mList << list[i].rgb();
qSort(mList);
return *this;
}
QList<QColor> ColourList::qcolorList() const
{
QList<QColor> list;
for (int i = 0, count = mList.count(); i < count; ++i)
list << QColor(mList[i]);
return list;
}
/*
* colourlist.h - an ordered list of colours
* Program: kalarm
* Copyright © 2003,2005,2007 by David Jarvie <software@astrojar.org.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef COLOURLIST_H
#define COLOURLIST_H
#include <QtAlgorithms>
#include <QColor>
#include <QList>
/**
* @short Represents a sorted list of colours.
*
* The ColourList class holds a list of colours, sorted in RGB value order.
*
* It provides a sorted QList of colours in RGB value order, with iterators
* and other access methods which return either QRgb or QColor objects.
*
* @author David Jarvie <software@astrojar.org.uk>
*/
class ColourList
{
public:
/** Constructs an empty list. */
ColourList() { }
/** Copy constructor. */
ColourList(const ColourList& l) : mList(l.mList) { }
/** Constructs a list whose values are preset to the colours in @p list. */
ColourList(const QList<QRgb>& list) : mList(list) { qSort(mList); }
/** Constructs a list whose values are preset to the colours in the @p list.
* Terminate @p list by an invalid colour.
*/
ColourList(const QColor* list);
/** Assignment operator. */
ColourList& operator=(const ColourList& l) { mList = l.mList; return *this; }
/** Sets the list to comprise the colours in @p list. */
ColourList& operator=(const QList<QRgb>& list) { mList = list; qSort(mList); return *this; }
/** Sets the list to comprise the colours in @p list. */
ColourList& operator=(const QList<QColor>& list);
/** Removes all values from the list. */
void clear() { mList.clear(); }
/** Adds the specified colour @p c to the list, in the correct sorted position. */
void insert(const QColor& c);
/** Removes the colour @p c from the list. */
void remove(const QColor& c) { mList.removeAt(mList.indexOf(c.rgb())); }
/** Adds the specified colour @p c to the list. */
ColourList& operator+=(const QColor& c) { insert(c); return *this; }
ColourList& operator<<(const QColor& c) { insert(c); return *this; }
/** Adds the colours in @p list to this list. */
ColourList& operator+=(const ColourList& list) { mList += list.mList; qSort(mList); return *this; }
ColourList& operator<<(const ColourList& list) { mList += list.mList; qSort(mList); return *this; }
/** Returns true if the colours in the two lists are the same. */
bool operator==(const ColourList& l) const { return mList == l.mList; }
/** Returns true if the colours in the two lists differ. */
bool operator!=(const ColourList& l) const { return mList != l.mList; }
/** Returns the number of colours in the list. */
int count() const { return mList.count(); }
/** Returns true if the list is empty. */
bool isEmpty() const { return mList.isEmpty(); }
/** Returns true if the list contains the colour @p c. */
bool contains(const QColor& c) const { return mList.contains(c.rgb()); }
/** Finds the first occurrence of colour @p c in the list.
* @return index to first occurrence of @p c, or -1 if @p c is not in the list
*/
int indexOf(const QColor& c, int from = 0) const { return mList.indexOf(c.rgb(), from); }
/** Returns the colour at position @p i in the list. The item must exist. */
QColor operator[](int i) const { return QColor(mList[i]); }
/** Returns the list as a list of QColor objects. */
QList<QColor> qcolorList() const;
private:
QList<QRgb> mList;
};
#endif // COLOURLIST_H
......@@ -43,7 +43,6 @@
#include <kapplication.h>
#include <kiconloader.h>
#include <kcombobox.h>
#include <kcolorcombo.h>
#include <KStandardGuiItem>
#include <ksystemtimezone.h>
#include <kicon.h>
......@@ -61,6 +60,7 @@ using namespace LibKHolidays;
#include "alarmresources.h"
#include "alarmtimewidget.h"
#include "buttongroup.h"
#include "colourbutton.h"
#include "editdlg.h"
#include "editdlgtypes.h"
#include "fontcolour.h"
......@@ -1126,7 +1126,7 @@ QString EmailPrefTab::validateAddr(ButtonGroup* group, KLineEdit* addr, const QS
FontColourPrefTab::FontColourPrefTab()
: PrefsTabBase()
{
mFontChooser = new FontColourChooser(topWidget(), QStringList(), i18nc("@title:group", "Message Font && Color"), true, false);
mFontChooser = new FontColourChooser(topWidget(), QStringList(), i18nc("@title:group", "Message Font && Color"), true);
QHBoxLayout* hlayout = new QHBoxLayout();
hlayout->setMargin(0);
......@@ -1141,7 +1141,7 @@ FontColourPrefTab::FontColourPrefTab()
colourLayout->addWidget(box);
QLabel* label1 = new QLabel(i18nc("@label:listbox", "Disabled alarm color:"), box);
box->setStretchFactor(new QWidget(box), 0);
mDisabledColour = new KColorCombo(box);
mDisabledColour = new ColourButton(box);
label1->setBuddy(mDisabledColour);
box->setWhatsThis(i18nc("@info:whatsthis", "Choose the text color in the alarm list for disabled alarms."));
......@@ -1151,7 +1151,7 @@ FontColourPrefTab::FontColourPrefTab()
colourLayout->addWidget(box);
QLabel* label2 = new QLabel(i18nc("@label:listbox", "Archived alarm color:"), box);
box->setStretchFactor(new QWidget(box), 0);
mArchivedColour = new KColorCombo(box);
mArchivedColour = new ColourButton(box);
label2->setBuddy(mArchivedColour);
box->setWhatsThis(i18nc("@info:whatsthis", "Choose the text color in the alarm list for archived alarms."));
......@@ -1160,8 +1160,8 @@ FontColourPrefTab::FontColourPrefTab()
void FontColourPrefTab::restore(bool)
{
mFontChooser->setFgColour(Preferences::defaultFgColour());
mFontChooser->setBgColour(Preferences::defaultBgColour());
mFontChooser->setColours(Preferences::messageColours());