kalarm
fontcolour.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <QGroupBox>
00022 #include <QPushButton>
00023 #include <QLabel>
00024 #include <QVBoxLayout>
00025 #include <QHBoxLayout>
00026
00027 #include <kglobal.h>
00028 #include <kfontchooser.h>
00029 #include <kfontdialog.h>
00030 #include <kcolordialog.h>
00031 #include <khbox.h>
00032
00033 #include "kalarmapp.h"
00034 #include "preferences.h"
00035 #include "colourbutton.h"
00036 #include "checkbox.h"
00037 #include "fontcolour.moc"
00038
00039
00040 FontColourChooser::FontColourChooser(QWidget *parent,
00041 const QStringList &fontList, const QString& frameLabel, bool fg, bool defaultFont, int visibleListSize)
00042 : QWidget(parent),
00043 mFgColourButton(0),
00044 mReadOnly(false)
00045 {
00046 QVBoxLayout* topLayout = new QVBoxLayout(this);
00047 topLayout->setMargin(0);
00048 topLayout->setSpacing(KDialog::spacingHint());
00049 QWidget* page = this;
00050 if (!frameLabel.isNull())
00051 {
00052 page = new QGroupBox(frameLabel, this);
00053 topLayout->addWidget(page);
00054 topLayout = new QVBoxLayout(page);
00055 topLayout->setMargin(KDialog::marginHint());
00056 topLayout->setSpacing(KDialog::spacingHint());
00057 }
00058 QHBoxLayout* hlayout = new QHBoxLayout();
00059 hlayout->setMargin(0);
00060 topLayout->addLayout(hlayout);
00061 QVBoxLayout* colourLayout = new QVBoxLayout();
00062 colourLayout->setMargin(0);
00063 hlayout->addLayout(colourLayout);
00064 if (fg)
00065 {
00066 KHBox* box = new KHBox(page);
00067 box->setMargin(0);
00068 box->setSpacing(KDialog::spacingHint()/2);
00069 colourLayout->addWidget(box);
00070
00071 QLabel* label = new QLabel(i18nc("@label:listbox", "Foreground color:"), box);
00072 box->setStretchFactor(new QWidget(box), 0);
00073 mFgColourButton = new ColourButton(box);
00074 connect(mFgColourButton, SIGNAL(changed(const QColor&)), SLOT(setSampleColour()));
00075 label->setBuddy(mFgColourButton);
00076 box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message foreground color"));
00077 }
00078
00079 KHBox* box = new KHBox(page);
00080 box->setMargin(0);
00081 box->setSpacing(KDialog::spacingHint()/2);
00082 colourLayout->addWidget(box);
00083
00084 QLabel* label = new QLabel(i18nc("@label:listbox", "Background color:"), box);
00085 box->setStretchFactor(new QWidget(box), 0);
00086 mBgColourButton = new ColourButton(box);
00087 connect(mBgColourButton, SIGNAL(changed(const QColor&)), SLOT(setSampleColour()));
00088 label->setBuddy(mBgColourButton);
00089 box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
00090 hlayout->addStretch();
00091
00092 if (defaultFont)
00093 {
00094 QHBoxLayout* layout = new QHBoxLayout();
00095 layout->setMargin(0);
00096 topLayout->addLayout(layout);
00097 mDefaultFont = new CheckBox(i18nc("@option:check", "Use default font"), page);
00098 mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
00099 connect(mDefaultFont, SIGNAL(toggled(bool)), SLOT(slotDefaultFontToggled(bool)));
00100 mDefaultFont->setWhatsThis(i18nc("@info:whatsthis", "Check to use the default font current at the time the alarm is displayed."));
00101 layout->addWidget(mDefaultFont);
00102 layout->addWidget(new QWidget(page));
00103 }
00104 else
00105 mDefaultFont = 0;
00106
00107 mFontChooser = new KFontChooser(page, KFontChooser::DisplayFrame, fontList, visibleListSize);
00108 mFontChooser->installEventFilter(this);
00109 QList<QWidget*> kids = mFontChooser->findChildren<QWidget*>();
00110 for (int i = 0, end = kids.count(); i < end; ++i)
00111 kids[i]->installEventFilter(this);
00112 topLayout->addWidget(mFontChooser);
00113
00114 slotDefaultFontToggled(false);
00115 }
00116
00117 void FontColourChooser::setDefaultFont()
00118 {
00119 if (mDefaultFont)
00120 mDefaultFont->setChecked(true);
00121 }
00122
00123 void FontColourChooser::setFont(const QFont& font, bool onlyFixed)
00124 {
00125 if (mDefaultFont)
00126 mDefaultFont->setChecked(false);
00127 mFontChooser->setFont(font, onlyFixed);
00128 }
00129
00130 bool FontColourChooser::defaultFont() const
00131 {
00132 return mDefaultFont ? mDefaultFont->isChecked() : false;
00133 }
00134
00135 QFont FontColourChooser::font() const
00136 {
00137 return (mDefaultFont && mDefaultFont->isChecked()) ? QFont() : mFontChooser->font();
00138 }
00139
00140 void FontColourChooser::setBgColour(const QColor& colour)
00141 {
00142 mBgColourButton->setColor(colour);
00143 mFontChooser->setBackgroundColor(colour);
00144 }
00145
00146 void FontColourChooser::setSampleColour()
00147 {
00148 QColor bg = mBgColourButton->color();
00149 mFontChooser->setBackgroundColor(bg);
00150 QColor fg = fgColour();
00151 mFontChooser->setColor(fg);
00152 }
00153
00154 QColor FontColourChooser::bgColour() const
00155 {
00156 return mBgColourButton->color();
00157 }
00158
00159 QColor FontColourChooser::fgColour() const
00160 {
00161 if (mFgColourButton)
00162 return mFgColourButton->color();
00163 else
00164 {
00165 QColor bg = mBgColourButton->color();
00166 QPalette pal(bg, bg);
00167 return pal.color(QPalette::Active, QPalette::Text);
00168 }
00169 }
00170
00171 QString FontColourChooser::sampleText() const
00172 {
00173 return mFontChooser->sampleText();
00174 }
00175
00176 void FontColourChooser::setSampleText(const QString& text)
00177 {
00178 mFontChooser->setSampleText(text);
00179 }
00180
00181 void FontColourChooser::setFgColour(const QColor& colour)
00182 {
00183 if (mFgColourButton)
00184 {
00185 mFgColourButton->setColor(colour);
00186 mFontChooser->setColor(colour);
00187 }
00188 }
00189
00190 void FontColourChooser::setReadOnly(bool ro)
00191 {
00192 if (ro != mReadOnly)
00193 {
00194 mReadOnly = ro;
00195 if (mFgColourButton)
00196 mFgColourButton->setReadOnly(ro);
00197 mBgColourButton->setReadOnly(ro);
00198 mDefaultFont->setReadOnly(ro);
00199 }
00200 }
00201
00202 bool FontColourChooser::eventFilter(QObject*, QEvent* e)
00203 {
00204 if (mReadOnly)
00205 {
00206 switch (e->type())
00207 {
00208 case QEvent::MouseMove:
00209 case QEvent::MouseButtonPress:
00210 case QEvent::MouseButtonRelease:
00211 case QEvent::MouseButtonDblClick:
00212 case QEvent::KeyPress:
00213 case QEvent::KeyRelease:
00214 return true;
00215 default:
00216 break;
00217 }
00218 }
00219 return false;
00220 }
00221
00222 void FontColourChooser::slotDefaultFontToggled(bool on)
00223 {
00224 mFontChooser->setEnabled(!on);
00225 }