kalarm

specialactions.cpp

00001 /*
00002  *  specialactions.cpp  -  widget to specify special alarm actions
00003  *  Program:  kalarm
00004  *  Copyright (c) 2004, 2005 by David Jarvie <software@astrojar.org.uk>
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 <qlabel.h>
00024 #include <qlayout.h>
00025 #include <qwhatsthis.h>
00026 
00027 #include <klineedit.h>
00028 #include <kapplication.h>
00029 #include <kaboutdata.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 
00033 #include "functions.h"
00034 #include "shellprocess.h"
00035 #include "specialactions.moc"
00036 
00037 
00038 /*=============================================================================
00039 = Class SpecialActionsButton
00040 = Button to display the Special Alarm Actions dialogue.
00041 =============================================================================*/
00042 
00043 SpecialActionsButton::SpecialActionsButton(const QString& caption, QWidget* parent, const char* name)
00044     : QPushButton(caption, parent, name),
00045       mReadOnly(false)
00046 {
00047     connect(this, SIGNAL(clicked()), SLOT(slotButtonPressed()));
00048     QWhatsThis::add(this,
00049           i18n("Specify actions to execute before and after the alarm is displayed."));
00050 }
00051 
00052 /******************************************************************************
00053 *  Set the pre- and post-alarm actions.
00054 *  The button's pressed state is set to reflect whether any actions are set.
00055 */
00056 void SpecialActionsButton::setActions(const QString& pre, const QString& post)
00057 {
00058     mPreAction  = pre;
00059     mPostAction = post;
00060     setDown(!mPreAction.isEmpty() || !mPostAction.isEmpty());
00061 }
00062 
00063 /******************************************************************************
00064 *  Called when the OK button is clicked.
00065 *  Display a font and colour selection dialog and get the selections.
00066 */
00067 void SpecialActionsButton::slotButtonPressed()
00068 {
00069     SpecialActionsDlg dlg(mPreAction, mPostAction,
00070                       i18n("Special Alarm Actions"), this, "actionsDlg");
00071     dlg.setReadOnly(mReadOnly);
00072     if (dlg.exec() == QDialog::Accepted)
00073     {
00074         setActions(dlg.preAction(), dlg.postAction());
00075         emit selected();
00076     }
00077 }
00078 
00079 
00080 /*=============================================================================
00081 = Class SpecialActionsDlg
00082 = Pre- and post-alarm actions dialogue.
00083 =============================================================================*/
00084 
00085 static const char SPEC_ACT_DIALOG_NAME[] = "SpecialActionsDialog";
00086 
00087 
00088 SpecialActionsDlg::SpecialActionsDlg(const QString& preAction, const QString& postAction,
00089                                      const QString& caption, QWidget* parent, const char* name)
00090     : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false)
00091 {
00092     QWidget* page = new QWidget(this);
00093     setMainWidget(page);
00094     QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());
00095 
00096     mActions = new SpecialActions(page);
00097     mActions->setActions(preAction, postAction);
00098     layout->addWidget(mActions);
00099     layout->addSpacing(KDialog::spacingHint());
00100 
00101     QSize s;
00102     if (KAlarm::readConfigWindowSize(SPEC_ACT_DIALOG_NAME, s))
00103         resize(s);
00104 }
00105 
00106 /******************************************************************************
00107 *  Called when the OK button is clicked.
00108 */
00109 void SpecialActionsDlg::slotOk()
00110 {
00111     if (mActions->isReadOnly())
00112         reject();
00113     accept();
00114 }
00115 
00116 /******************************************************************************
00117 *  Called when the dialog's size has changed.
00118 *  Records the new size in the config file.
00119 */
00120 void SpecialActionsDlg::resizeEvent(QResizeEvent* re)
00121 {
00122     if (isVisible())
00123         KAlarm::writeConfigWindowSize(SPEC_ACT_DIALOG_NAME, re->size());
00124     KDialog::resizeEvent(re);
00125 }
00126 
00127 
00128 /*=============================================================================
00129 = Class SpecialActions
00130 = Pre- and post-alarm actions widget.
00131 =============================================================================*/
00132 
00133 SpecialActions::SpecialActions(QWidget* parent, const char* name)
00134     : QGroupBox(parent, name)
00135 {
00136     setFrameStyle(QFrame::NoFrame);
00137     init(QString::null);
00138 }
00139 
00140 SpecialActions::SpecialActions(const QString& frameLabel, QWidget* parent, const char* name)
00141     : QGroupBox(frameLabel, parent, name)
00142 {
00143     init(frameLabel);
00144 }
00145 
00146 void SpecialActions::init(const QString& frameLabel)
00147 {
00148     mReadOnly = false;
00149 
00150     QBoxLayout* topLayout = new QVBoxLayout(this, 0, KDialog::spacingHint());
00151     if (!frameLabel.isEmpty())
00152     {
00153         topLayout->setMargin(KDialog::marginHint());
00154         topLayout->addSpacing(fontMetrics().lineSpacing()/2);
00155     }
00156 
00157     // Pre-alarm action
00158     QLabel* label = new QLabel(i18n("Pre-a&larm action:"), this);
00159     label->setFixedSize(label->sizeHint());
00160     topLayout->addWidget(label, 0, Qt::AlignAuto);
00161 
00162     mPreAction = new KLineEdit(this);
00163     label->setBuddy(mPreAction);
00164     QWhatsThis::add(mPreAction,
00165           i18n("Enter a shell command to execute before the alarm is displayed. "
00166                "N.B. KAlarm will wait for the command to complete before displaying the alarm."));
00167     topLayout->addWidget(mPreAction);
00168     topLayout->addSpacing(KDialog::spacingHint());
00169 
00170     // Post-alarm action
00171     label = new QLabel(i18n("Post-alar&m action:"), this);
00172     label->setFixedSize(label->sizeHint());
00173     topLayout->addWidget(label, 0, Qt::AlignAuto);
00174 
00175     mPostAction = new KLineEdit(this);
00176     label->setBuddy(mPostAction);
00177     QWhatsThis::add(mPostAction, i18n("Enter a shell command to execute after the alarm window is closed."));
00178     topLayout->addWidget(mPostAction);
00179 }
00180 
00181 void SpecialActions::setActions(const QString& pre, const QString& post)
00182 {
00183     mPreAction->setText(pre);
00184     mPostAction->setText(post);
00185 }
00186 
00187 QString SpecialActions::preAction() const
00188 {
00189     return mPreAction->text();
00190 }
00191 
00192 QString SpecialActions::postAction() const
00193 {
00194     return mPostAction->text();
00195 }
00196 
00197 void SpecialActions::setReadOnly(bool ro)
00198 {
00199     mReadOnly = ro;
00200     mPreAction->setReadOnly(mReadOnly);
00201     mPostAction->setReadOnly(mReadOnly);
00202 }
KDE Home | KDE Accessibility Home | Description of Access Keys