Mercurial > projects > qtd
comparison examples/desktop/systray/window.d @ 350:31520b2c0b3c
Removed dependency on parent trait and stringof
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Thu, 20 May 2010 15:49:08 +0300 |
parents | 4e31cbd9e20c |
children |
comparison
equal
deleted
inserted
replaced
349:925386e0e780 | 350:31520b2c0b3c |
---|---|
60 | 60 |
61 class Window : public QDialog | 61 class Window : public QDialog |
62 { | 62 { |
63 public: | 63 public: |
64 | 64 |
65 this() | 65 this() |
66 { | 66 { |
67 createIconGroupBox(); | 67 createIconGroupBox(); |
68 createMessageGroupBox(); | 68 createMessageGroupBox(); |
69 | 69 |
70 iconLabel.setMinimumWidth(durationLabel.sizeHint().width()); | 70 iconLabel.setMinimumWidth(durationLabel.sizeHint().width()); |
71 | 71 |
72 createActions(); | 72 createActions(); |
73 createTrayIcon(); | 73 createTrayIcon(); |
74 | 74 |
75 connect(showMessageButton, "clicked", this, "showMessage"); | 75 connect(showMessageButton, "clicked", this, "showMessage"); |
76 connect(showIconCheckBox, "toggled", trayIcon, "setVisible"); | 76 connect(showIconCheckBox, "toggled", trayIcon, "setVisible"); |
77 connect(iconComboBox, "currentIndexChanged", this, "setIcon"); | 77 connect(iconComboBox, "currentIndexChanged", this, "setIcon"); |
78 connect(trayIcon, "messageClicked", this, "messageClicked"); | 78 connect(trayIcon, "messageClicked", this, "messageClicked"); |
79 connect(trayIcon, "activated", this, "iconActivated"); | 79 connect(trayIcon, "activated", this, "iconActivated"); |
80 | 80 |
81 QVBoxLayout mainLayout = new QVBoxLayout; | 81 QVBoxLayout mainLayout = new QVBoxLayout; |
82 mainLayout.addWidget(iconGroupBox); | 82 mainLayout.addWidget(iconGroupBox); |
83 mainLayout.addWidget(messageGroupBox); | 83 mainLayout.addWidget(messageGroupBox); |
84 setLayout(mainLayout); | 84 setLayout(mainLayout); |
85 | 85 |
86 iconComboBox.setCurrentIndex(1); | 86 iconComboBox.setCurrentIndex(1); |
87 trayIcon.show(); | 87 trayIcon.show(); |
88 | 88 |
89 setWindowTitle(tr("Systray")); | 89 setWindowTitle(tr("Systray")); |
90 resize(400, 300); | 90 resize(400, 300); |
91 } | 91 } |
92 | 92 |
93 void setVisible(bool visible) | 93 void setVisible(bool visible) |
94 { | 94 { |
95 minimizeAction.setEnabled(visible); | 95 minimizeAction.setEnabled(visible); |
96 maximizeAction.setEnabled(!isMaximized()); | 96 maximizeAction.setEnabled(!isMaximized()); |
97 restoreAction.setEnabled(isMaximized() || !visible); | 97 restoreAction.setEnabled(isMaximized() || !visible); |
98 QDialog.setVisible(visible); | 98 QDialog.setVisible(visible); |
99 } | 99 } |
100 | 100 |
101 protected: | 101 protected: |
102 | 102 |
103 void closeEvent(QCloseEvent event) | 103 void closeEvent(QCloseEvent event) |
104 { | 104 { |
105 if (trayIcon.isVisible()) { | 105 if (trayIcon.isVisible()) { |
106 QMessageBox.information(this, tr("Systray"), | 106 QMessageBox.information(this, tr("Systray"), |
107 tr("The program will keep running in the system tray. To terminate the program, " | 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.")); | 108 "choose <b>Quit</b> in the context menu of the system tray entry.")); |
109 hide(); | 109 hide(); |
110 event.ignore(); | 110 event.ignore(); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 private: // slots | 114 private: // slots |
115 | 115 |
116 void slot_setIcon(int index) | 116 void slot_setIcon(int index) |
117 { | 117 { |
118 QIcon icon = iconComboBox.itemIcon(index); | 118 QIcon icon = iconComboBox.itemIcon(index); |
119 trayIcon.setIcon(icon); | 119 trayIcon.setIcon(icon); |
120 setWindowIcon(icon); | 120 setWindowIcon(icon); |
121 | 121 |
122 trayIcon.setToolTip(iconComboBox.itemText(index)); | 122 trayIcon.setToolTip(iconComboBox.itemText(index)); |
123 } | 123 } |
124 | 124 |
125 void slot_iconActivated(QSystemTrayIcon.ActivationReason reason) | 125 void slot_iconActivated(QSystemTrayIcon.ActivationReason reason) |
126 { | 126 { |
127 switch (reason) { | 127 switch (reason) { |
128 case QSystemTrayIcon.Trigger: | 128 case QSystemTrayIcon.Trigger: |
129 case QSystemTrayIcon.DoubleClick: | 129 case QSystemTrayIcon.DoubleClick: |
130 iconComboBox.setCurrentIndex((iconComboBox.currentIndex() + 1) % iconComboBox.count()); | 130 iconComboBox.setCurrentIndex((iconComboBox.currentIndex() + 1) % iconComboBox.count()); |
131 break; | 131 break; |
132 case QSystemTrayIcon.MiddleClick: | 132 case QSystemTrayIcon.MiddleClick: |
133 showMessage(); | 133 showMessage(); |
134 break; | 134 break; |
135 default: | 135 default: |
136 } | 136 } |
137 } | 137 } |
138 | 138 |
139 void slot_showMessage() | 139 void slot_showMessage() |
140 { | 140 { |
141 QSystemTrayIcon.MessageIcon icon = cast(QSystemTrayIcon.MessageIcon) | 141 QSystemTrayIcon.MessageIcon icon = cast(QSystemTrayIcon.MessageIcon) |
142 typeComboBox.itemData(typeComboBox.currentIndex()).toInt(); | 142 typeComboBox.itemData(typeComboBox.currentIndex()).toInt(); |
143 trayIcon.showMessage(titleEdit.text(), bodyEdit.toPlainText(), icon, durationSpinBox.value() * 1000); | 143 trayIcon.showMessage(titleEdit.text(), bodyEdit.toPlainText(), icon, durationSpinBox.value() * 1000); |
144 } | 144 } |
145 | 145 |
146 void slot_messageClicked() | 146 void slot_messageClicked() |
147 { | 147 { |
148 QMessageBox.information(null, tr("Systray"), | 148 QMessageBox.information(null, tr("Systray"), |
149 tr("Sorry, I already gave what help I could.\nMaybe you should try asking a human?")); | 149 tr("Sorry, I already gave what help I could.\nMaybe you should try asking a human?")); |
150 } | 150 } |
151 | 151 |
152 private: | 152 private: |
153 | 153 |
154 void createIconGroupBox() | 154 void createIconGroupBox() |
155 { | 155 { |
156 iconGroupBox = new QGroupBox(tr("Tray Icon")); | 156 iconGroupBox = new QGroupBox(tr("Tray Icon")); |
157 | 157 |
158 iconLabel = new QLabel("Icon:"); | 158 iconLabel = new QLabel("Icon:"); |
159 | 159 |
160 iconComboBox = new QComboBox; | 160 iconComboBox = new QComboBox; |
161 iconComboBox.addItem(new QIcon(":/images/bad.svg"), tr("Bad")); | 161 iconComboBox.addItem(new QIcon(":/images/bad.svg"), tr("Bad")); |
162 iconComboBox.addItem(new QIcon(":/images/heart.svg"), tr("Heart")); | 162 iconComboBox.addItem(new QIcon(":/images/heart.svg"), tr("Heart")); |
163 iconComboBox.addItem(new QIcon(":/images/trash.svg"), tr("Trash")); | 163 iconComboBox.addItem(new QIcon(":/images/trash.svg"), tr("Trash")); |
164 | 164 |
165 showIconCheckBox = new QCheckBox(tr("Show icon")); | 165 showIconCheckBox = new QCheckBox(tr("Show icon")); |
166 showIconCheckBox.setChecked(true); | 166 showIconCheckBox.setChecked(true); |
167 | 167 |
168 QHBoxLayout iconLayout = new QHBoxLayout; | 168 QHBoxLayout iconLayout = new QHBoxLayout; |
169 iconLayout.addWidget(iconLabel); | 169 iconLayout.addWidget(iconLabel); |
170 iconLayout.addWidget(iconComboBox); | 170 iconLayout.addWidget(iconComboBox); |
171 iconLayout.addStretch(); | 171 iconLayout.addStretch(); |
172 iconLayout.addWidget(showIconCheckBox); | 172 iconLayout.addWidget(showIconCheckBox); |
173 iconGroupBox.setLayout(iconLayout); | 173 iconGroupBox.setLayout(iconLayout); |
174 } | 174 } |
175 | 175 |
176 void createMessageGroupBox() | 176 void createMessageGroupBox() |
177 { | 177 { |
178 messageGroupBox = new QGroupBox(tr("Balloon Message")); | 178 messageGroupBox = new QGroupBox(tr("Balloon Message")); |
179 | 179 |
180 typeLabel = new QLabel(tr("Type:")); | 180 typeLabel = new QLabel(tr("Type:")); |
181 | 181 |
182 typeComboBox = new QComboBox; | 182 typeComboBox = new QComboBox; |
183 typeComboBox.addItem(tr("None"), new QVariant(cast(ulong) QSystemTrayIcon.NoIcon)); | 183 typeComboBox.addItem(tr("None"), new QVariant(cast(ulong) QSystemTrayIcon.NoIcon)); |
184 typeComboBox.addItem(style().standardIcon( | 184 typeComboBox.addItem(style().standardIcon( |
185 QStyle.SP_MessageBoxInformation), tr("Information"), | 185 QStyle.SP_MessageBoxInformation), tr("Information"), |
186 new QVariant(cast(ulong) QSystemTrayIcon.Information)); | 186 new QVariant(cast(ulong) QSystemTrayIcon.Information)); |
187 typeComboBox.addItem(style().standardIcon( | 187 typeComboBox.addItem(style().standardIcon( |
188 QStyle.SP_MessageBoxWarning), tr("Warning"), | 188 QStyle.SP_MessageBoxWarning), tr("Warning"), |
189 new QVariant(cast(ulong) QSystemTrayIcon.Warning)); | 189 new QVariant(cast(ulong) QSystemTrayIcon.Warning)); |
190 typeComboBox.addItem(style().standardIcon( | 190 typeComboBox.addItem(style().standardIcon( |
191 QStyle.SP_MessageBoxCritical), tr("Critical"), | 191 QStyle.SP_MessageBoxCritical), tr("Critical"), |
192 new QVariant(cast(ulong) QSystemTrayIcon.Critical)); | 192 new QVariant(cast(ulong) QSystemTrayIcon.Critical)); |
193 typeComboBox.setCurrentIndex(1); | 193 typeComboBox.setCurrentIndex(1); |
194 | 194 |
195 durationLabel = new QLabel(tr("Duration:")); | 195 durationLabel = new QLabel(tr("Duration:")); |
196 | 196 |
197 durationSpinBox = new QSpinBox; | 197 durationSpinBox = new QSpinBox; |
198 durationSpinBox.setRange(5, 60); | 198 durationSpinBox.setRange(5, 60); |
199 durationSpinBox.setSuffix(" s"); | 199 durationSpinBox.setSuffix(" s"); |
200 durationSpinBox.setValue(15); | 200 durationSpinBox.setValue(15); |
201 | 201 |
202 durationWarningLabel = new QLabel(tr("(some systems might ignore this hint)")); | 202 durationWarningLabel = new QLabel(tr("(some systems might ignore this hint)")); |
203 durationWarningLabel.setIndent(10); | 203 durationWarningLabel.setIndent(10); |
204 | 204 |
205 titleLabel = new QLabel(tr("Title:")); | 205 titleLabel = new QLabel(tr("Title:")); |
206 | 206 |
207 titleEdit = new QLineEdit(tr("Cannot connect to network")); | 207 titleEdit = new QLineEdit(tr("Cannot connect to network")); |
208 | 208 |
209 bodyLabel = new QLabel(tr("Body:")); | 209 bodyLabel = new QLabel(tr("Body:")); |
210 | 210 |
211 bodyEdit = new QTextEdit; | 211 bodyEdit = new QTextEdit; |
212 bodyEdit.setPlainText(tr("Don't believe me. Honestly, I don't have a clue.\nClick this balloon for details.")); | 212 bodyEdit.setPlainText(tr("Don't believe me. Honestly, I don't have a clue.\nClick this balloon for details.")); |
213 | 213 |
214 showMessageButton = new QPushButton(tr("Show Message")); | 214 showMessageButton = new QPushButton(tr("Show Message")); |
215 showMessageButton.setDefault(true); | 215 showMessageButton.setDefault(true); |
216 | 216 |
217 QGridLayout messageLayout = new QGridLayout; | 217 QGridLayout messageLayout = new QGridLayout; |
218 messageLayout.addWidget(typeLabel, 0, 0); | 218 messageLayout.addWidget(typeLabel, 0, 0); |
219 messageLayout.addWidget(typeComboBox, 0, 1, 1, 2); | 219 messageLayout.addWidget(typeComboBox, 0, 1, 1, 2); |
220 messageLayout.addWidget(durationLabel, 1, 0); | 220 messageLayout.addWidget(durationLabel, 1, 0); |
221 messageLayout.addWidget(durationSpinBox, 1, 1); | 221 messageLayout.addWidget(durationSpinBox, 1, 1); |
222 messageLayout.addWidget(durationWarningLabel, 1, 2, 1, 3); | 222 messageLayout.addWidget(durationWarningLabel, 1, 2, 1, 3); |
223 messageLayout.addWidget(titleLabel, 2, 0); | 223 messageLayout.addWidget(titleLabel, 2, 0); |
224 messageLayout.addWidget(titleEdit, 2, 1, 1, 4); | 224 messageLayout.addWidget(titleEdit, 2, 1, 1, 4); |
225 messageLayout.addWidget(bodyLabel, 3, 0); | 225 messageLayout.addWidget(bodyLabel, 3, 0); |
226 messageLayout.addWidget(bodyEdit, 3, 1, 2, 4); | 226 messageLayout.addWidget(bodyEdit, 3, 1, 2, 4); |
227 messageLayout.addWidget(showMessageButton, 5, 4); | 227 messageLayout.addWidget(showMessageButton, 5, 4); |
228 messageLayout.setColumnStretch(3, 1); | 228 messageLayout.setColumnStretch(3, 1); |
229 messageLayout.setRowStretch(4, 1); | 229 messageLayout.setRowStretch(4, 1); |
230 messageGroupBox.setLayout(messageLayout); | 230 messageGroupBox.setLayout(messageLayout); |
231 } | 231 } |
232 | 232 |
233 void createActions() | 233 void createActions() |
234 { | 234 { |
235 minimizeAction = new QAction(tr("Mi&nimize"), this); | 235 minimizeAction = new QAction(tr("Mi&nimize"), this); |
236 connect(minimizeAction, "triggered", this, "hide"); | 236 connect(minimizeAction, "triggered", this, "hide"); |
237 | 237 |
238 maximizeAction = new QAction(tr("Ma&ximize"), this); | 238 maximizeAction = new QAction(tr("Ma&ximize"), this); |
239 connect(maximizeAction, "triggered", this, "showMaximized"); | 239 connect(maximizeAction, "triggered", this, "showMaximized"); |
240 | 240 |
241 restoreAction = new QAction(tr("&Restore"), this); | 241 restoreAction = new QAction(tr("&Restore"), this); |
242 connect(restoreAction, "triggered", this, "showNormal"); | 242 connect(restoreAction, "triggered", this, "showNormal"); |
243 | 243 |
244 quitAction = new QAction(tr("&Quit"), this); | 244 quitAction = new QAction(tr("&Quit"), this); |
245 connect(quitAction, "triggered", qApp(), "quit"); | 245 connect(quitAction, "triggered", qApp(), "quit"); |
246 } | 246 } |
247 | 247 |
248 void createTrayIcon() | 248 void createTrayIcon() |
249 { | 249 { |
250 trayIconMenu = new QMenu(this); | 250 trayIconMenu = new QMenu(this); |
251 trayIconMenu.addAction(minimizeAction); | 251 trayIconMenu.addAction(minimizeAction); |
252 trayIconMenu.addAction(maximizeAction); | 252 trayIconMenu.addAction(maximizeAction); |
253 trayIconMenu.addAction(restoreAction); | 253 trayIconMenu.addAction(restoreAction); |
254 trayIconMenu.addSeparator(); | 254 trayIconMenu.addSeparator(); |
255 trayIconMenu.addAction(quitAction); | 255 trayIconMenu.addAction(quitAction); |
256 | 256 |
257 trayIcon = new QSystemTrayIcon(this); | 257 trayIcon = new QSystemTrayIcon(this); |
258 trayIcon.setContextMenu(trayIconMenu); | 258 trayIcon.setContextMenu(trayIconMenu); |
259 } | 259 } |
260 | 260 |
261 QGroupBox iconGroupBox; | 261 QGroupBox iconGroupBox; |
262 QLabel iconLabel; | 262 QLabel iconLabel; |
263 QComboBox iconComboBox; | 263 QComboBox iconComboBox; |
264 QCheckBox showIconCheckBox; | 264 QCheckBox showIconCheckBox; |
265 | 265 |
266 QGroupBox messageGroupBox; | 266 QGroupBox messageGroupBox; |
267 QLabel typeLabel; | 267 QLabel typeLabel; |
268 QLabel durationLabel; | 268 QLabel durationLabel; |
269 QLabel durationWarningLabel; | 269 QLabel durationWarningLabel; |
270 QLabel titleLabel; | 270 QLabel titleLabel; |
271 QLabel bodyLabel; | 271 QLabel bodyLabel; |
272 QComboBox typeComboBox; | 272 QComboBox typeComboBox; |
273 QSpinBox durationSpinBox; | 273 QSpinBox durationSpinBox; |
274 QLineEdit titleEdit; | 274 QLineEdit titleEdit; |
275 QTextEdit bodyEdit; | 275 QTextEdit bodyEdit; |
276 QPushButton showMessageButton; | 276 QPushButton showMessageButton; |
277 | 277 |
278 QAction minimizeAction; | 278 QAction minimizeAction; |
279 QAction maximizeAction; | 279 QAction maximizeAction; |
280 QAction restoreAction; | 280 QAction restoreAction; |
281 QAction quitAction; | 281 QAction quitAction; |
282 | 282 |
283 QSystemTrayIcon trayIcon; | 283 QSystemTrayIcon trayIcon; |
284 QMenu trayIconMenu; | 284 QMenu trayIconMenu; |
285 | 285 |
286 mixin Q_OBJECT; | 286 mixin Q_OBJECT; |
287 } | 287 } |