• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
pickfileradio.cpp
Go to the documentation of this file.
1 /*
2  * pickfileradio.cpp - radio button with an associated file picker
3  * Program: kalarm
4  * Copyright © 2005,2009 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 #include "pickfileradio.h"
23 
24 #include "buttongroup.h"
25 #include "lineedit.h"
26 
27 #include <kdebug.h>
28 
29 #include <QPushButton>
30 #include <QTimer>
31 
32 
33 PickFileRadio::PickFileRadio(QPushButton* button, LineEdit* edit, const QString& text, ButtonGroup* group, QWidget* parent)
34  : RadioButton(text, parent),
35  mGroup(group),
36  mEdit(0),
37  mLastButton(0),
38  mRevertButton(false)
39 {
40  Q_ASSERT(parent);
41  init(button, edit);
42 }
43 
44 PickFileRadio::PickFileRadio(const QString& text, ButtonGroup* group, QWidget* parent)
45  : RadioButton(text, parent),
46  mGroup(group),
47  mEdit(0),
48  mButton(0),
49  mLastButton(0),
50  mRevertButton(false)
51 {
52  Q_ASSERT(parent);
53 }
54 
55 void PickFileRadio::init(QPushButton* button, LineEdit* edit)
56 {
57  Q_ASSERT(button);
58  if (mEdit)
59  mEdit->disconnect(this);
60  mEdit = edit;
61  mButton = button;
62  mButton->setEnabled(false);
63  connect(mButton, SIGNAL(clicked()), SLOT(slotPickFile()));
64  if (mEdit)
65  {
66  mEdit->setEnabled(false);
67  connect(mEdit, SIGNAL(textChanged(QString)), SIGNAL(fileChanged()));
68  }
69  connect(mGroup, SIGNAL(buttonSet(QAbstractButton*)), SLOT(slotSelectionChanged(QAbstractButton*)));
70  setReadOnly(RadioButton::isReadOnly());
71 }
72 
73 void PickFileRadio::setReadOnly(bool ro)
74 {
75  RadioButton::setReadOnly(ro);
76  if (mButton)
77  {
78  if (mEdit)
79  mEdit->setReadOnly(ro);
80  if (ro)
81  mButton->hide();
82  else
83  mButton->show();
84  }
85 }
86 
87 void PickFileRadio::setFile(const QString& file)
88 {
89  mFile = file;
90 }
91 
92 QString PickFileRadio::file() const
93 {
94  return mEdit ? mEdit->text() : mFile;
95 }
96 
97 /******************************************************************************
98 * Set the radio button enabled or disabled.
99 * Adjusts the enabled/disabled state of other controls appropriately.
100 */
101 void PickFileRadio::setEnabled(bool enable)
102 {
103  Q_ASSERT(mButton);
104  RadioButton::setEnabled(enable);
105  enable = enable && mGroup->checkedButton() == this;
106  if (enable)
107  {
108  if (!pickFileIfNone())
109  enable = false; // revert to previously selected type
110  }
111  mButton->setEnabled(enable);
112  if (mEdit)
113  mEdit->setEnabled(enable);
114 }
115 
116 /******************************************************************************
117 * Called when the selected radio button changes.
118 */
119 void PickFileRadio::slotSelectionChanged(QAbstractButton* button)
120 {
121  if (button == mLastButton || mRevertButton)
122  return;
123  if (mLastButton == this)
124  {
125  mButton->setEnabled(false);
126  if (mEdit)
127  mEdit->setEnabled(false);
128  }
129  else if (button == this)
130  {
131  if (!pickFileIfNone())
132  return; // revert to previously selected type
133  mButton->setEnabled(true);
134  if (mEdit)
135  mEdit->setEnabled(true);
136  }
137  mLastButton = button;
138 }
139 
140 /******************************************************************************
141 * Prompt for a file name if there is none currently entered.
142 */
143 bool PickFileRadio::pickFileIfNone()
144 {
145  if (mEdit)
146  mFile = mEdit->text();
147  if (!mFile.isEmpty())
148  return true;
149  return !slotPickFile().isEmpty();
150 }
151 
152 /******************************************************************************
153 * Called when the file picker button is clicked.
154 * Reply = mFile, or null string if dialogue, and 'this', was deleted.
155 */
156 QString PickFileRadio::slotPickFile()
157 {
158  // To avoid crashes on application quit, we need to check whether the
159  // dialogue, and hence this PickFileRadio, was deleted while active,
160  // before accessing class members.
161  QString file = pickFile();
162  if (file.isNull())
163  return file; // 'this' is probably invalid now
164  if (!file.isEmpty())
165  {
166  mFile = file;
167  if (mEdit)
168  mEdit->setText(mFile);
169  }
170  if (mFile.isEmpty())
171  {
172  // No file is selected, so revert to the previous radio button selection.
173  // But wait a moment before setting the radio button, or it won't work.
174  mRevertButton = true; // prevent picker dialog popping up twice
175  QTimer::singleShot(0, this, SLOT(setLastButton()));
176  }
177  return mFile;
178 }
179 
180 /******************************************************************************
181 * Select the previously selected radio button in the group.
182 */
183 void PickFileRadio::setLastButton()
184 {
185  if (!mLastButton)
186  setChecked(false); // we don't know the previous selection, so just turn this button off
187  else
188  mLastButton->setChecked(true);
189  mRevertButton = false;
190 }
191 #include "moc_pickfileradio.cpp"
192 // vim: et sw=4:
PickFileRadio::init
void init(QPushButton *button, LineEdit *edit=0)
Initialises the widget.
Definition: pickfileradio.cpp:55
QWidget
buttongroup.h
RadioButton::isReadOnly
bool isReadOnly() const
LineEdit::setText
virtual void setText(const QString &str)
PickFileRadio::fileChanged
void fileChanged()
text
virtual QByteArray text(quint32 serialNumber) const =0
RadioButton
lineedit.h
pickfileradio.h
radio button with an associated file picker
LineEdit::text
QString text() const
QString::isNull
bool isNull() const
PickFileRadio::pickFile
virtual QString pickFile()=0
Chooses a file, for example by displaying a file selection dialog.
QWidget::setEnabled
void setEnabled(bool)
LineEdit
QString::isEmpty
bool isEmpty() const
QAbstractButton::clicked
void clicked(bool checked)
PickFileRadio::setEnabled
virtual void setEnabled(bool)
Enables or disables the radio button, and adjusts the enabled state of the associated browse button a...
Definition: pickfileradio.cpp:101
PickFileRadio::file
QString file() const
Returns the currently selected file name.
Definition: pickfileradio.cpp:92
QString
QWidget::hide
void hide()
PickFileRadio::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the radio button and associated widgets are read-only for the user.
Definition: pickfileradio.cpp:73
PickFileRadio::PickFileRadio
PickFileRadio(QPushButton *button, LineEdit *edit, const QString &text, ButtonGroup *group, QWidget *parent)
Constructor.
Definition: pickfileradio.cpp:33
QAbstractButton::setChecked
void setChecked(bool)
QAbstractButton
RadioButton::setReadOnly
virtual void setReadOnly(bool readOnly)
PickFileRadio::setFile
void setFile(const QString &file)
Notifies the widget of the currently selected file name.
Definition: pickfileradio.cpp:87
kalarm.h
QPushButton
QWidget::show
void show()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QButtonGroup::checkedButton
QAbstractButton * checkedButton() const
QTimer::singleShot
singleShot
ButtonGroup
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal