comparison examples/desktop/systray/window.d @ 120:2c9ef955a171

add desktop examples
author mandel
date Fri, 05 Jun 2009 11:52:21 +0000
parents
children 256ab6cb8e85
comparison
equal deleted inserted replaced
119:97650cedf5d7 120:2c9ef955a171
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 module window;
42
43 import qt.gui.QSystemTrayIcon;
44 import qt.gui.QDialog;
45 import qt.gui.QAction;
46 import qt.gui.QCheckBox;
47 import qt.gui.QComboBox;
48 import qt.gui.QGroupBox;
49 import qt.gui.QLabel;
50 import qt.gui.QLineEdit;
51 import qt.gui.QMenu;
52 import qt.gui.QPushButton;
53 import qt.gui.QSpinBox;
54 import qt.gui.QTextEdit;
55 import qt.gui.QVBoxLayout;
56 import qt.gui.QHBoxLayout;
57 import qt.gui.QMessageBox;
58 import qt.gui.QGridLayout;
59
60
61 class Window : public QDialog
62 {
63 public:
64
65 this()
66 {
67 createIconGroupBox();
68 createMessageGroupBox();
69
70 iconLabel.setMinimumWidth(durationLabel.sizeHint().width());
71
72 createActions();
73 createTrayIcon();
74
75 showMessageButton.clicked.connect(&this.showMessage);
76 showIconCheckBox.toggled.connect(&trayIcon.setVisible);
77 iconComboBox.currentIndexChanged.connect(&this.setIcon);
78 trayIcon.messageClicked.connect(&this.messageClicked);
79 trayIcon.activated.connect(&this.iconActivated);
80
81 QVBoxLayout mainLayout = new QVBoxLayout;
82 mainLayout.addWidget(iconGroupBox);
83 mainLayout.addWidget(messageGroupBox);
84 setLayout(mainLayout);
85
86 iconComboBox.setCurrentIndex(1);
87 trayIcon.show();
88
89 setWindowTitle(tr("Systray"));
90 resize(400, 300);
91 }
92
93 void setVisible(bool visible)
94 {
95 minimizeAction.setEnabled(visible);
96 maximizeAction.setEnabled(!isMaximized());
97 restoreAction.setEnabled(isMaximized() || !visible);
98 QDialog.setVisible(visible);
99 }
100
101 protected:
102
103 void closeEvent(QCloseEvent event)
104 {
105 if (trayIcon.isVisible()) {
106 QMessageBox.information(this, tr("Systray"),
107 tr("The program will keep running in the system tray. To terminate the program, "
108 "choose <b>Quit</b> in the context menu of the system tray entry."));
109 hide();
110 event.ignore();
111 }
112 }
113
114 private:
115
116 void setIcon(int index)
117 {
118 QIcon icon = iconComboBox.itemIcon(index);
119 trayIcon.setIcon(icon);
120 setWindowIcon(icon);
121
122 trayIcon.setToolTip(iconComboBox.itemText(index));
123 }
124
125 void iconActivated(QSystemTrayIcon.ActivationReason reason)
126 {
127 switch (reason) {
128 case QSystemTrayIcon.Trigger:
129 case QSystemTrayIcon.DoubleClick:
130 iconComboBox.setCurrentIndex((iconComboBox.currentIndex() + 1) % iconComboBox.count());
131 break;
132 case QSystemTrayIcon.MiddleClick:
133 showMessage();
134 break;
135 default:
136 }
137 }
138
139 void showMessage()
140 {
141 QSystemTrayIcon.MessageIcon icon = cast(QSystemTrayIcon.MessageIcon)
142 typeComboBox.itemData(typeComboBox.currentIndex()).toInt();
143 trayIcon.showMessage(titleEdit.text(), bodyEdit.toPlainText(), icon, durationSpinBox.value() * 1000);
144 }
145
146 void messageClicked()
147 {
148 QMessageBox.information(null, tr("Systray"),
149 tr("Sorry, I already gave what help I could.\nMaybe you should try asking a human?"));
150 }
151
152 private:
153
154 void createIconGroupBox()
155 {
156 iconGroupBox = new QGroupBox(tr("Tray Icon"));
157
158 iconLabel = new QLabel("Icon:");
159
160 iconComboBox = new QComboBox;
161 iconComboBox.addItem(new QIcon(":/images/bad.svg"), tr("Bad"));
162 iconComboBox.addItem(new QIcon(":/images/heart.svg"), tr("Heart"));
163 iconComboBox.addItem(new QIcon(":/images/trash.svg"), tr("Trash"));
164
165 showIconCheckBox = new QCheckBox(tr("Show icon"));
166 showIconCheckBox.setChecked(true);
167
168 QHBoxLayout iconLayout = new QHBoxLayout;
169 iconLayout.addWidget(iconLabel);
170 iconLayout.addWidget(iconComboBox);
171 iconLayout.addStretch();
172 iconLayout.addWidget(showIconCheckBox);
173 iconGroupBox.setLayout(iconLayout);
174 }
175
176 void createMessageGroupBox()
177 {
178 messageGroupBox = new QGroupBox(tr("Balloon Message"));
179
180 typeLabel = new QLabel(tr("Type:"));
181
182 typeComboBox = new QComboBox;
183 typeComboBox.addItem(tr("None"), new QVariant(cast(ulong) QSystemTrayIcon.NoIcon));
184 typeComboBox.addItem(style().standardIcon(
185 QStyle.SP_MessageBoxInformation), tr("Information"),
186 new QVariant(cast(ulong) QSystemTrayIcon.Information));
187 typeComboBox.addItem(style().standardIcon(
188 QStyle.SP_MessageBoxWarning), tr("Warning"),
189 new QVariant(cast(ulong) QSystemTrayIcon.Warning));
190 typeComboBox.addItem(style().standardIcon(
191 QStyle.SP_MessageBoxCritical), tr("Critical"),
192 new QVariant(cast(ulong) QSystemTrayIcon.Critical));
193 typeComboBox.setCurrentIndex(1);
194
195 durationLabel = new QLabel(tr("Duration:"));
196
197 durationSpinBox = new QSpinBox;
198 durationSpinBox.setRange(5, 60);
199 durationSpinBox.setSuffix(" s");
200 durationSpinBox.setValue(15);
201
202 durationWarningLabel = new QLabel(tr("(some systems might ignore this hint)"));
203 durationWarningLabel.setIndent(10);
204
205 titleLabel = new QLabel(tr("Title:"));
206
207 titleEdit = new QLineEdit(tr("Cannot connect to network"));
208
209 bodyLabel = new QLabel(tr("Body:"));
210
211 bodyEdit = new QTextEdit;
212 bodyEdit.setPlainText(tr("Don't believe me. Honestly, I don't have a clue.\nClick this balloon for details."));
213
214 showMessageButton = new QPushButton(tr("Show Message"));
215 showMessageButton.setDefault(true);
216
217 QGridLayout messageLayout = new QGridLayout;
218 messageLayout.addWidget(typeLabel, 0, 0);
219 messageLayout.addWidget(typeComboBox, 0, 1, 1, 2);
220 messageLayout.addWidget(durationLabel, 1, 0);
221 messageLayout.addWidget(durationSpinBox, 1, 1);
222 messageLayout.addWidget(durationWarningLabel, 1, 2, 1, 3);
223 messageLayout.addWidget(titleLabel, 2, 0);
224 messageLayout.addWidget(titleEdit, 2, 1, 1, 4);
225 messageLayout.addWidget(bodyLabel, 3, 0);
226 messageLayout.addWidget(bodyEdit, 3, 1, 2, 4);
227 messageLayout.addWidget(showMessageButton, 5, 4);
228 messageLayout.setColumnStretch(3, 1);
229 messageLayout.setRowStretch(4, 1);
230 messageGroupBox.setLayout(messageLayout);
231 }
232
233 void createActions()
234 {
235 minimizeAction = new QAction(tr("Mi&nimize"), this);
236 minimizeAction.triggered.connect(&this.hide);
237
238 maximizeAction = new QAction(tr("Ma&ximize"), this);
239 maximizeAction.triggered.connect(&this.showMaximized);
240
241 restoreAction = new QAction(tr("&Restore"), this);
242 restoreAction.triggered.connect(&this.showNormal);
243
244 quitAction = new QAction(tr("&Quit"), this);
245 quitAction.triggered.connect(&QApplication.quit);
246 }
247
248 void createTrayIcon()
249 {
250 trayIconMenu = new QMenu(this);
251 trayIconMenu.addAction(minimizeAction);
252 trayIconMenu.addAction(maximizeAction);
253 trayIconMenu.addAction(restoreAction);
254 trayIconMenu.addSeparator();
255 trayIconMenu.addAction(quitAction);
256
257 trayIcon = new QSystemTrayIcon(this);
258 trayIcon.setContextMenu(trayIconMenu);
259 }
260
261 QGroupBox iconGroupBox;
262 QLabel iconLabel;
263 QComboBox iconComboBox;
264 QCheckBox showIconCheckBox;
265
266 QGroupBox messageGroupBox;
267 QLabel typeLabel;
268 QLabel durationLabel;
269 QLabel durationWarningLabel;
270 QLabel titleLabel;
271 QLabel bodyLabel;
272 QComboBox typeComboBox;
273 QSpinBox durationSpinBox;
274 QLineEdit titleEdit;
275 QTextEdit bodyEdit;
276 QPushButton showMessageButton;
277
278 QAction minimizeAction;
279 QAction maximizeAction;
280 QAction restoreAction;
281 QAction quitAction;
282
283 QSystemTrayIcon trayIcon;
284 QMenu trayIconMenu;
285 }