~ubuntu-branches/ubuntu/utopic/kradio/utopic

« back to all changes in this revision

Viewing changes to kradio3/src/radiostation-listview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marc 'HE' Brockschmidt
  • Date: 2008-03-16 19:00:02 UTC
  • mfrom: (3.1.2 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080316190002-sdjqu8cahhx7c6tk
Tags: 0.1.1.1~20061112-3.1
* Non-maintainer upload.
* Fix gcc-4.3 FTBFS, patch by Kibi (Closes: #455390)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          radiostation-listview.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Mi Feb 3 2004
 
5
    copyright            : (C) 2003 by Martin Witte
 
6
    email                : witte@kawo1.rwth-aachen.de
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include "include/radiostation-listview.h"
 
19
#include "include/stationlist.h"
 
20
#include "include/radiostation.h"
 
21
#include "include/station-drag-object.h"
 
22
 
 
23
#include <klocale.h>
 
24
#include <qfile.h>
 
25
#include <qimage.h>
 
26
#include <qpixmap.h>
 
27
 
 
28
#include <kconfig.h>
 
29
 
 
30
RadioStationListView::RadioStationListView(QWidget *parent, const char *name)
 
31
  : KListView(parent, name)
 
32
{
 
33
    addColumn(i18n("No."));
 
34
    addColumn(i18n("Icon"));
 
35
    addColumn(i18n("Station"));
 
36
    addColumn(i18n("Description"));
 
37
    setAllColumnsShowFocus(true);
 
38
    setSorting(-1);
 
39
 
 
40
    QObject::connect(this, SIGNAL(spacePressed(QListViewItem*)),
 
41
                     this, SLOT(slotStationActivation(QListViewItem* )));
 
42
    QObject::connect(this, SIGNAL(returnPressed(QListViewItem*)),
 
43
                     this, SLOT(slotStationActivation(QListViewItem* )));
 
44
    QObject::connect(this, SIGNAL(doubleClicked(QListViewItem*)),
 
45
                     this, SLOT(slotStationActivation(QListViewItem *)));
 
46
    QObject::connect(this, SIGNAL(currentChanged(QListViewItem*)),
 
47
                     this, SLOT(slotCurrentStationChanged(QListViewItem *)));
 
48
 
 
49
    setAcceptDrops(true);
 
50
}
 
51
 
 
52
 
 
53
RadioStationListView::~RadioStationListView()
 
54
{
 
55
}
 
56
 
 
57
 
 
58
QListViewItem *RadioStationListView::getItemForIndex(int idx) const
 
59
{
 
60
    QListViewItem *item = NULL;
 
61
 
 
62
    if (idx >= 0 && idx < childCount()) {
 
63
        item = firstChild();
 
64
        int i = 0;
 
65
        while (item && i < idx) {
 
66
            item = item->nextSibling();
 
67
            ++i;
 
68
        }
 
69
    }
 
70
    return item;
 
71
}
 
72
 
 
73
 
 
74
int RadioStationListView::getIndexForItem(QListViewItem *queryItem) const
 
75
{
 
76
    int idx = -1;
 
77
 
 
78
    if (queryItem) {
 
79
        QListViewItem *item = firstChild();
 
80
        ++idx;
 
81
        while (item && item != queryItem) {
 
82
            item = item->nextSibling();
 
83
            ++idx;
 
84
        }
 
85
        if (!item)
 
86
            idx = -1;
 
87
    }
 
88
 
 
89
    return idx;
 
90
}
 
91
 
 
92
 
 
93
void RadioStationListView::setStation(int idx, const RadioStation &s, int nr)
 
94
{
 
95
    QListViewItem *item = getItemForIndex(idx);
 
96
 
 
97
    if (idx < 0) {
 
98
        item = new QListViewItem(this, firstChild());
 
99
        firstChild()->moveItem(item);
 
100
        m_StationIDs.prepend(s.stationID());
 
101
        idx = 0;
 
102
    } else if (idx >= childCount()) {
 
103
        item = new QListViewItem(this, lastChild());
 
104
        m_StationIDs.append(s.stationID());
 
105
        idx = childCount() - 1;
 
106
    }
 
107
 
 
108
    if (item) {
 
109
        item->setDragEnabled(true);
 
110
        item->setDropEnabled(true);
 
111
 
 
112
        item->setText(0, QString::number(nr > 0 ? nr : idx+1));
 
113
        item->setText(2, s.name());
 
114
        item->setText(3, s.description());
 
115
 
 
116
        m_StationIDs[idx] = s.stationID();
 
117
 
 
118
        QImage  img(s.iconName());
 
119
        if (!img.isNull()) {
 
120
            int   h = img.height();
 
121
            float f = 0.9 * (float)(item->height()) / (h ? (float)h : 1.0);
 
122
            item->setPixmap(1, img.smoothScale((int)(img.width()*f), (int)(h * f)));
 
123
        } else {
 
124
            item->setPixmap(1, QPixmap());
 
125
        }
 
126
    }
 
127
}
 
128
 
 
129
 
 
130
void RadioStationListView::appendStation(const RadioStation &st, int nr)
 
131
{
 
132
    setStation(childCount(), st, nr);
 
133
}
 
134
 
 
135
 
 
136
void RadioStationListView::setStations(const StationList &stations)
 
137
{
 
138
    clear();
 
139
    for (RawStationList::Iterator it(stations.all()); it.current(); ++it) {
 
140
        setStation(childCount(), *it.current());
 
141
    }
 
142
}
 
143
 
 
144
 
 
145
void RadioStationListView::removeStation(int idx)
 
146
{
 
147
    QListViewItem *item = getItemForIndex(idx);
 
148
    if (item) {
 
149
        delete item;
 
150
        m_StationIDs.remove(m_StationIDs.at(idx));
 
151
    }
 
152
}
 
153
 
 
154
void RadioStationListView::takeItem(QListViewItem *item, int idx)
 
155
{
 
156
    QListView::takeItem(item);
 
157
    m_StationIDs.remove(m_StationIDs.at(idx));
 
158
}
 
159
 
 
160
void RadioStationListView::insertItem(QListViewItem *item, const QString &stationid, int idx_to)
 
161
{
 
162
    QListView::insertItem(item);
 
163
    m_StationIDs.insert(m_StationIDs.at(idx_to), stationid);
 
164
}
 
165
 
 
166
void RadioStationListView::setCurrentStation(int idx)
 
167
{
 
168
    QListViewItem *item = getItemForIndex(idx);
 
169
    if (item) {
 
170
        clearSelection();
 
171
        setSelected(item, true);
 
172
        setCurrentItem(item);
 
173
    }
 
174
}
 
175
 
 
176
 
 
177
int RadioStationListView::currentStationIndex() const
 
178
{
 
179
    return getIndexForItem(currentItem());
 
180
}
 
181
 
 
182
 
 
183
void RadioStationListView::slotStationActivation(QListViewItem *item)
 
184
{
 
185
    emit sigStationActivated(getIndexForItem(item));
 
186
}
 
187
 
 
188
 
 
189
void RadioStationListView::slotCurrentStationChanged(QListViewItem *item)
 
190
{
 
191
    emit sigCurrentStationChanged(getIndexForItem(item));
 
192
}
 
193
 
 
194
 
 
195
void RadioStationListView::saveState (KConfig *cfg) const
 
196
{
 
197
    if (!cfg)
 
198
        return;
 
199
    for (int i = 0; i < 4; ++i)
 
200
        cfg->writeEntry(QString(name()) + "_radiostation_listview_col_" + QString::number(i), columnWidth(i));
 
201
}
 
202
 
 
203
 
 
204
void RadioStationListView::restoreState (KConfig *cfg)
 
205
{
 
206
    if (!cfg)
 
207
        return;
 
208
    for (int i = 0; i < 4; ++i)
 
209
        setColumnWidth(i, cfg->readNumEntry(QString(name()) + "_radiostation_listview_col_" + QString::number(i), -1));
 
210
}
 
211
 
 
212
 
 
213
QDragObject *RadioStationListView::dragObject()
 
214
{
 
215
    QStringList list;
 
216
    QListViewItem *item = firstChild();
 
217
    for (int idx = 0; item; ++idx, item = item->nextSibling()) {
 
218
        if (item->isSelected()) {
 
219
            list.append(m_StationIDs[idx]);
 
220
        }
 
221
    }
 
222
    return new StationDragObject(list, this);
 
223
}
 
224
 
 
225
void RadioStationListView::dragEnterEvent(QDragEnterEvent* event)
 
226
{
 
227
    event->accept(StationDragObject::canDecode(event));
 
228
}
 
229
 
 
230
void RadioStationListView::contentsDragEnterEvent(QDragEnterEvent* event)
 
231
{
 
232
    bool a = StationDragObject::canDecode(event);
 
233
    if (a)
 
234
        IErrorLogClient::staticLogDebug(i18n("contentsDragEnterEvent accepted"));
 
235
    else
 
236
        IErrorLogClient::staticLogDebug(i18n("contentsDragEnterEvent rejected"));
 
237
    event->accept(a);
 
238
}
 
239
 
 
240
void RadioStationListView::dropEvent(QDropEvent* event)
 
241
{
 
242
    QStringList list;
 
243
 
 
244
    if ( StationDragObject::decode(event, list) ) {
 
245
        emit sigStationsReceived(list);
 
246
    }
 
247
}
 
248
 
 
249
void RadioStationListView::contentsDropEvent(QDropEvent* event)
 
250
{
 
251
    dropEvent(event);
 
252
}
 
253
 
 
254
void RadioStationListView::contentsDragMoveEvent(QDragMoveEvent* event)
 
255
{
 
256
    event->accept();
 
257
}
 
258
 
 
259
#include "radiostation-listview.moc"