[e8cdcd]: plugins / gui-quickbar / quickbar.cpp Maximize Restore History

Download this file

quickbar.cpp    399 lines (314 with data), 10.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/***************************************************************************
quickbar.cpp - description
-------------------
begin : Mon Feb 11 2002
copyright : (C) 2002 by Martin Witte / Frank Schwanz
email : emw-kradio@nocabal.de / schwanz@fh-brandenburg.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtGui/QToolTip>
#include <QtGui/QToolButton>
#include <QtGui/QButtonGroup>
#include <QtGui/QResizeEvent>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
//#include <QtCore/QNameSpace>
//#include <ktoolbarbutton.h>
#include <kwindowinfo.h>
#include <kwindowsystem.h>
#include <klocale.h>
#include <kglobal.h>
#include <kconfiggroup.h>
#include <kaboutdata.h>
#include <kbuttongroup.h>
#include <kicon.h>
//#include "aboutwidget.h"
#include "station-drag-object.h"
#include "stationlist.h"
#include "radiostation.h"
#include "buttonflowlayout4.h"
#include "quickbar-configuration.h"
#include "quickbar.h"
///////////////////////////////////////////////////////////////////////
//// plugin library functions
PLUGIN_LIBRARY_FUNCTIONS(QuickBar, PROJECT_NAME, i18n("Radio Station Quick Selection Toolbar"));
/////////////////////////////////////////////////////////////////////////////
QuickBar::QuickBar(const QString &instanceID, const QString &name)
: QWidget(),
WidgetPluginBase(this, instanceID, name, i18n("Quickbar Plugin")),
m_layout(NULL),
m_showShortName(true),
m_ignoreNoticeActivation(false)
{
QObject::connect(&m_mapper, SIGNAL(mapped(const QString &)), this, SLOT(buttonToggled(const QString &)));
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
autoSetCaption();
setAcceptDrops(true);
}
QuickBar::~QuickBar()
{
}
bool QuickBar::connectI(Interface *i)
{
bool a = IRadioClient::connectI(i);
bool b = IStationSelection::connectI(i);
bool c = PluginBase::connectI(i);
return a || b || c;
}
bool QuickBar::disconnectI(Interface *i)
{
bool a = IRadioClient::disconnectI(i);
bool b = IStationSelection::disconnectI(i);
bool c = PluginBase::disconnectI(i);
return a || b || c;
}
// IStationSelection
bool QuickBar::setStationSelection(const QStringList &sl)
{
if (m_stationIDs != sl) {
m_stationIDs = sl;
rebuildGUI();
notifyStationSelectionChanged(m_stationIDs);
}
return true;
}
// PluginBase methods
void QuickBar::restoreState (const KConfigGroup &config)
{
WidgetPluginBase::restoreState(config, false);
int nStations = config.readEntry("nStations", 0);
m_stationIDs.clear();
for (int i = 1; i <= nStations; ++i) {
QString s = config.readEntry(QString("stationID-") + QString().setNum(i), QString());
if (s.length())
m_stationIDs += s;
}
rebuildGUI();
notifyStationSelectionChanged(m_stationIDs);
}
void QuickBar::saveState (KConfigGroup &config) const
{
WidgetPluginBase::saveState(config);
config.writeEntry("nStations", m_stationIDs.size());
int i = 1;
QStringList::const_iterator end = m_stationIDs.end();
for (QStringList::const_iterator it = m_stationIDs.begin(); it != end; ++it, ++i) {
config.writeEntry(QString("stationID-") + QString().setNum(i), *it);
}
}
ConfigPageInfo QuickBar::createConfigurationPage()
{
QuickbarConfiguration *conf = new QuickbarConfiguration(NULL);
connectI (conf);
return ConfigPageInfo(
conf,
i18n("Quickbar"),
i18n("Quickbar Configuration"),
"view-list-icons"
);
}
/*AboutPageInfo QuickBar::createAboutPage()
{*/
/* KAboutData aboutData("kradio4",
NULL,
NULL,
I18N_NOOP("Quickback for KRadio"),
KAboutData::License_GPL,
"(c) 2002-2005 Martin Witte, Klas Kalass",
0,
"http://sourceforge.net/projects/kradio",
0);
aboutData.addAuthor("Martin Witte", "", "emw-kradio@nocabal.de");
aboutData.addAuthor("Klas Kalass", "", "klas.kalass@gmx.de");
return AboutPageInfo(
new KRadioAboutWidget(aboutData, KRadioAboutWidget::AbtTabbed),
i18n("Quickbar"),
i18n("Quickbar Plugin"),
"view-list-icons"
);*/
// return AboutPageInfo();
// }
// IRadio methods
bool QuickBar::noticePowerChanged(bool /*on*/)
{
activateCurrentButton();
autoSetCaption();
return true;
}
bool QuickBar::noticeStationChanged (const RadioStation &rs, int /*idx*/)
{
if (!m_ignoreNoticeActivation)
activateButton(rs);
autoSetCaption();
return true;
}
bool QuickBar::noticeStationsChanged(const StationList &/*sl*/)
{
// FIXME
// we can remove no longer existent stationIDs,
// but it doesn't matter if we don't care.
rebuildGUI();
return true;
}
// button management methods
void QuickBar::uncheckAllOtherButtons(const QToolButton *b)
{
bool blocked = m_mapper.signalsBlocked();
m_mapper.blockSignals(true);
QToolButton *otherbtn;
foreach (otherbtn, m_buttons) {
if (otherbtn != b) {
otherbtn->setChecked(false);
}
}
m_mapper.blockSignals(blocked);
}
void QuickBar::buttonToggled(const QString &stationID)
{
QToolButton *b = static_cast<QToolButton*>(m_mapper.mapping(stationID));
if (b) {
if (b->isChecked()) {
uncheckAllOtherButtons(b);
const StationList &sl = queryStations();
const RadioStation &rs = sl.stationWithID(stationID);
bool old = m_ignoreNoticeActivation;
m_ignoreNoticeActivation = true;
sendActivateStation(rs);
m_ignoreNoticeActivation = old;
sendPowerOn();
} else {
if (queryIsPowerOn() && stationID == queryCurrentStation().stationID()) {
sendPowerOff();
}
}
} else {
// something strange happened
}
}
void QuickBar::activateCurrentButton()
{
activateButton(queryCurrentStation());
}
void QuickBar::activateButton(const RadioStation &rs)
{
QToolButton *b = NULL;
if (rs.isValid() && queryIsPowerOn()) {
b = static_cast<QToolButton*>(m_mapper.mapping(rs.stationID()));
if (b) {
b->setChecked(true);
}
}
uncheckAllOtherButtons(b);
autoSetCaption();
}
void QuickBar::rebuildGUI()
{
if (m_layout) delete m_layout;
QToolButton *btn;
foreach (btn, m_buttons) {
delete btn;
}
m_buttons.clear();
m_layout = new ButtonFlowLayout4(this);
m_layout->setMargin(1);
m_layout->setSpacing(0);
const StationList &stations = queryStations();
QString stationID;
foreach (stationID, m_stationIDs) {
const RadioStation &rs = stations.stationWithID(stationID);
if (! rs.isValid()) continue;
QToolButton *b = new QToolButton(this);
m_buttons.append(b);
b->setCheckable(true);
if (rs.iconName().length()) {
b->setIcon(KIcon(rs.iconName()));
} else {
b->setText(m_showShortName ? rs.shortName() : rs.name());
}
b->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
b->setToolTip(rs.longName());
m_layout->addWidget(b);
b->show();
m_mapper.setMapping(b, stationID);
QObject::connect(b, SIGNAL(toggled(bool)), &m_mapper, SLOT(map()));
}
// activate correct button
activateCurrentButton();
// calculate geometry
if (m_layout) {
QRect r = geometry();
int h = m_layout->heightForWidth( r.width());
if (h > r.height())
setGeometry(r.x(), r.y(), r.width(), h);
}
}
void QuickBar::setVisible(bool v)
{
pSetVisible(v);
QWidget::setVisible(v);
}
void QuickBar::showEvent(QShowEvent *e)
{
QWidget::showEvent(e);
WidgetPluginBase::pShowEvent(e);
}
void QuickBar::hideEvent(QHideEvent *e)
{
QWidget::hideEvent(e);
WidgetPluginBase::pHideEvent(e);
}
void QuickBar::setGeometry (int x, int y, int w, int h)
{
if (m_layout) {
QSize marginSize(m_layout->margin()*2, m_layout->margin()*2);
setMinimumSize(m_layout->minimumSize(QSize(w, h) - marginSize) + marginSize);
}
QWidget::setGeometry (x, y, w, h);
}
void QuickBar::setGeometry (const QRect &r)
{
setGeometry (r.x(), r.y(), r.width(), r.height());
}
void QuickBar::resizeEvent (QResizeEvent *e)
{
// minimumSize might change because of the flow layout
if (m_layout) {
QSize marginSize(m_layout->margin()*2, m_layout->margin()*2);
setMinimumSize(m_layout->minimumSize(e->size() - marginSize) + marginSize);
}
QWidget::resizeEvent (e);
}
void QuickBar::autoSetCaption()
{
const RadioStation &rs = queryCurrentStation();
const QString caption = queryIsPowerOn() && rs.isValid() ? i18n("KRadio4 Quickbar, %1", rs.longName()) : i18n("KRadio4 Quickbar");
setWindowTitle(caption);
}
void QuickBar::dragEnterEvent(QDragEnterEvent* event)
{
bool a = StationDragObject::canDecode(event);
if (a)
IErrorLogClient::staticLogDebug(i18n("contentsDragEnterEvent accepted"));
else
IErrorLogClient::staticLogDebug(i18n("contentsDragEnterEvent rejected"));
event->accept();
}
void QuickBar::dropEvent(QDropEvent* event)
{
QStringList list;
if ( StationDragObject::decode(event, list) ) {
QStringList l = getStationSelection();
for (QList<QString>::const_iterator it = list.begin(); it != list.end(); ++it)
if (!l.contains(*it))
l.append(*it);
setStationSelection(l);
}
}
#include "quickbar.moc"