kalarm

fontcolourbutton.cpp

00001 /*
00002  *  fontcolourbutton.cpp  -  pushbutton widget to select a font and colour
00003  *  Program:  kalarm
00004  *  Copyright © 2003-2005,2007,2008 by David Jarvie <djarvie@kde.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <qcheckbox.h>
00024 #include <qlayout.h>
00025 #include <qwhatsthis.h>
00026 
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 
00030 #include "fontcolour.h"
00031 #include "pushbutton.h"
00032 #include "fontcolourbutton.moc"
00033 
00034 
00035 /*=============================================================================
00036 = Class FontColourButton
00037 = Font/colour selection button.
00038 =============================================================================*/
00039 
00040 FontColourButton::FontColourButton(QWidget* parent, const char* name)
00041     : QFrame(parent, name),
00042       mReadOnly(false)
00043 {
00044     setFrameStyle(NoFrame);
00045     QHBoxLayout* layout = new QHBoxLayout(this, 0, KDialog::spacingHint());
00046 
00047     mButton = new PushButton(i18n("Font && Co&lor..."), this);
00048     mButton->setFixedSize(mButton->sizeHint());
00049     connect(mButton, SIGNAL(clicked()), SLOT(slotButtonPressed()));
00050     QWhatsThis::add(mButton,
00051           i18n("Choose the font, and foreground and background color, for the alarm message."));
00052     layout->addWidget(mButton);
00053 
00054     // Font and colour sample display
00055     mSample = new QLineEdit(this);
00056     mSample->setMinimumHeight(QMAX(mSample->fontMetrics().lineSpacing(), mButton->height()*3/2));
00057     mSample->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::MinimumExpanding);
00058     mSample->setText(i18n("The Quick Brown Fox Jumps Over The Lazy Dog"));
00059     mSample->setCursorPosition(0);
00060     mSample->setAlignment(Qt::AlignCenter);
00061     QWhatsThis::add(mSample,
00062           i18n("This sample text illustrates the current font and color settings. "
00063                "You may edit it to test special characters."));
00064     layout->addWidget(mSample);
00065 }
00066 
00067 void FontColourButton::setDefaultFont()
00068 {
00069     mDefaultFont = true;
00070     mSample->setFont(QFont());
00071 }
00072 
00073 void FontColourButton::setFont(const QFont& font)
00074 {
00075     mDefaultFont = false;
00076     mFont = font;
00077     mSample->setFont(mFont);
00078 }
00079 
00080 void FontColourButton::setBgColour(const QColor& colour)
00081 {
00082     mBgColour = colour;
00083     mSample->setPaletteBackgroundColor(mBgColour);
00084 }
00085 
00086 void FontColourButton::setFgColour(const QColor& colour)
00087 {
00088     mFgColour = colour;
00089     mSample->setPaletteForegroundColor(mFgColour);
00090 }
00091 
00092 /******************************************************************************
00093 *  Called when the OK button is clicked.
00094 *  Display a font and colour selection dialog and get the selections.
00095 */
00096 void FontColourButton::slotButtonPressed()
00097 {
00098     FontColourDlg dlg(mBgColour, mFgColour, mFont, mDefaultFont,
00099                       i18n("Choose Alarm Font & Color"), this, "fontColourDlg");
00100     dlg.setReadOnly(mReadOnly);
00101     if (dlg.exec() == QDialog::Accepted)
00102     {
00103         mDefaultFont = dlg.defaultFont();
00104         mFont        = dlg.font();
00105         mSample->setFont(mFont);
00106         mBgColour    = dlg.bgColour();
00107         mSample->setPaletteBackgroundColor(mBgColour);
00108         mFgColour    = dlg.fgColour();
00109         mSample->setPaletteForegroundColor(mFgColour);
00110         emit selected();
00111     }
00112 }
00113 
00114 
00115 /*=============================================================================
00116 = Class FontColourDlg
00117 = Font/colour selection dialog.
00118 =============================================================================*/
00119 
00120 FontColourDlg::FontColourDlg(const QColor& bgColour, const QColor& fgColour, const QFont& font,
00121                              bool defaultFont, const QString& caption, QWidget* parent, const char* name)
00122     : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false),
00123       mReadOnly(false)
00124 {
00125     QWidget* page = new QWidget(this);
00126     setMainWidget(page);
00127     QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());
00128     mChooser = new FontColourChooser(page, 0, false, QStringList(), QString::null, false, true, true);
00129     mChooser->setBgColour(bgColour);
00130     mChooser->setFgColour(fgColour);
00131     if (defaultFont)
00132         mChooser->setDefaultFont();
00133     else
00134         mChooser->setFont(font);
00135     layout->addWidget(mChooser);
00136     layout->addSpacing(KDialog::spacingHint());
00137 }
00138 
00139 /******************************************************************************
00140 *  Called when the OK button is clicked.
00141 */
00142 void FontColourDlg::slotOk()
00143 {
00144     if (mReadOnly)
00145     {
00146         reject();
00147         return;
00148     }
00149     mDefaultFont = mChooser->defaultFont();
00150     mFont        = mChooser->font();
00151     mBgColour    = mChooser->bgColour();
00152     mFgColour    = mChooser->fgColour();
00153     accept();
00154 }
00155 
00156 void FontColourDlg::setReadOnly(bool ro)
00157 {
00158     mReadOnly = ro;
00159     mChooser->setReadOnly(mReadOnly);
00160 }
KDE Home | KDE Accessibility Home | Description of Access Keys