source: smplayer/trunk/src/favoriteeditor.cpp@ 136

Last change on this file since 136 was 124, checked in by Silvan Scherrer, 14 years ago

SMPlayer: 0.7.1 trunk update

  • Property svn:eol-style set to LF
File size: 10.6 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2012 Ricardo Villalba <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "favoriteeditor.h"
20#include "images.h"
21
22#include <QHeaderView>
23#include <QFileDialog>
24#include <QItemDelegate>
25#include "filechooser.h"
26
27#define COL_ICON 0
28#define COL_NAME 1
29#define COL_FILE 2
30
31#include <QItemDelegate>
32
33class FEDelegate : public QItemDelegate
34{
35public:
36 FEDelegate(QObject *parent = 0);
37
38 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
39 const QModelIndex &index) const;
40 virtual void setModelData(QWidget * editor, QAbstractItemModel * model,
41 const QModelIndex & index ) const;
42};
43
44FEDelegate::FEDelegate(QObject *parent) : QItemDelegate(parent) {
45}
46
47QWidget * FEDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
48 //qDebug("FEDelegate::createEditor");
49
50 if (index.column() == COL_FILE) {
51 FileChooser * fch = new FileChooser(parent);
52 fch->setOptions(QFileDialog::DontUseNativeDialog | QFileDialog::DontResolveSymlinks); // Crashes if use the KDE dialog
53 fch->setText( index.model()->data(index, Qt::DisplayRole).toString() );
54 return fch;
55 }
56 else
57 if (index.column() == COL_NAME) {
58 QLineEdit * e = new QLineEdit(parent);
59 e->setText( index.model()->data(index, Qt::DisplayRole).toString() );
60 return e;
61 }
62 else {
63 return QItemDelegate::createEditor(parent, option, index);
64 }
65}
66
67void FEDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
68 if (index.column() == COL_FILE) {
69 FileChooser * fch = static_cast<FileChooser*>(editor);
70 model->setData(index, fch->text() );
71 }
72 else
73 if (index.column() == COL_NAME) {
74 QLineEdit * e = static_cast<QLineEdit*>(editor);
75 model->setData(index, e->text() );
76 }
77}
78
79QString FavoriteEditor::last_dir;
80
81FavoriteEditor::FavoriteEditor( QWidget* parent, Qt::WindowFlags f )
82 : QDialog(parent, f)
83{
84 setupUi(this);
85
86 add_button->setIcon( Images::icon("bookmark_add") );
87 add_submenu_button->setIcon( Images::icon("bookmark_folder") );
88 delete_button->setIcon( Images::icon("delete") );
89 delete_all_button->setIcon( Images::icon("trash") );
90 up_button->setIcon( Images::icon("up") );
91 down_button->setIcon( Images::icon("down") );
92
93 table->setColumnCount(3);
94 table->setHorizontalHeaderLabels(QStringList() << tr("Icon") << tr("Name") << tr("Media") );
95
96 table->setAlternatingRowColors(true);
97 table->horizontalHeader()->setResizeMode(COL_FILE, QHeaderView::Stretch);
98
99 table->setSelectionBehavior(QAbstractItemView::SelectRows);
100 table->setSelectionMode(QAbstractItemView::ExtendedSelection);
101
102 table->setItemDelegateForColumn( COL_NAME, new FEDelegate(table) );
103 table->setItemDelegateForColumn( COL_FILE, new FEDelegate(table) );
104
105 connect(table, SIGNAL(cellActivated(int,int)), this, SLOT(edit_icon(int,int)));
106
107 setWindowTitle( tr("Favorite editor") );
108
109 setCaption( tr("Favorite list") );
110 setIntro( tr("You can edit, delete, sort or add new items. Double click on "
111 "a cell to edit its contents.") );
112
113 setDialogIcon( Images::icon("favorite") );
114}
115
116FavoriteEditor::~FavoriteEditor() {
117}
118
119void FavoriteEditor::setCaption(const QString & caption) {
120 caption_text = caption;
121 updateTitleLabel();
122}
123
124QString FavoriteEditor::caption() {
125 return caption_text;
126}
127
128void FavoriteEditor::setIntro(const QString & intro) {
129 intro_text = intro;
130 updateTitleLabel();
131}
132
133QString FavoriteEditor::intro() {
134 return intro_text;
135}
136
137void FavoriteEditor::updateTitleLabel() {
138 title_label->setText( "<h1>" + caption_text + "</h1>" + intro_text );
139}
140
141void FavoriteEditor::setDialogIcon( const QPixmap & icon ) {
142 dialog_icon->setPixmap(icon);
143}
144
145const QPixmap * FavoriteEditor::dialogIcon() const {
146 return dialog_icon->pixmap();
147}
148
149void FavoriteEditor::setData( FavoriteList list ) {
150 table->setRowCount(list.count());
151
152 for (int n = 0; n < list.count(); n++) {
153 QTableWidgetItem * icon_item = new QTableWidgetItem;
154 icon_item->setIcon( QIcon(list[n].icon()) );
155 icon_item->setData( Qt::UserRole, list[n].icon() );
156 icon_item->setData( Qt::ToolTipRole, list[n].icon() );
157 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
158
159 QTableWidgetItem * name_item = new QTableWidgetItem;
160 name_item->setText( list[n].name() );
161
162 QTableWidgetItem * file_item = new QTableWidgetItem;
163 file_item->setData( Qt::ToolTipRole, list[n].file() );
164 file_item->setData( Qt::UserRole, list[n].isSubentry() );
165 if (list[n].isSubentry()) {
166 file_item->setFlags(Qt::ItemIsSelectable);
167 file_item->setData( Qt::UserRole + 1, list[n].file() );
168 file_item->setText( tr("Favorite list") );
169 } else {
170 file_item->setText( list[n].file() );
171 }
172
173 table->setItem(n, COL_ICON, icon_item);
174 table->setItem(n, COL_NAME, name_item);
175 table->setItem(n, COL_FILE, file_item);
176 }
177
178 //table->resizeColumnsToContents();
179
180 //table->setCurrentCell(0, 0);
181 table->setCurrentCell(table->rowCount()-1, 0);
182}
183
184FavoriteList FavoriteEditor::data() {
185 FavoriteList list;
186
187 for (int n = 0; n < table->rowCount(); n++) {
188 Favorite f;
189 f.setName( table->item(n, COL_NAME)->text() );
190 f.setIcon( table->item(n, COL_ICON)->data(Qt::UserRole).toString() );
191 f.setSubentry( table->item(n, COL_FILE)->data(Qt::UserRole).toBool() );
192 if (f.isSubentry()) {
193 f.setFile( table->item(n, COL_FILE)->data(Qt::UserRole + 1).toString() );
194 } else {
195 f.setFile( table->item(n, COL_FILE)->text() );
196 }
197
198 list.append(f);
199 }
200
201 return list;
202}
203
204void FavoriteEditor::on_delete_button_clicked() {
205 int row = table->currentRow();
206 qDebug("FavoriteEditor::on_delete_button_clicked: current_row: %d", row);
207
208 if (row > -1) table->removeRow(row);
209
210 if (row >= table->rowCount()) row--;
211 table->setCurrentCell(row, table->currentColumn());
212}
213
214void FavoriteEditor::on_delete_all_button_clicked() {
215 qDebug("FavoriteEditor::on_delete_all_button_clicked");
216 table->setRowCount(0);
217}
218
219void FavoriteEditor::on_add_button_clicked() {
220 int row = table->currentRow();
221 qDebug("FavoriteEditor::on_add_button_clicked: current_row: %d", row);
222 row++;
223 table->insertRow(row);
224
225 QTableWidgetItem * icon_item = new QTableWidgetItem;
226 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
227
228 table->setItem(row, COL_ICON, icon_item);
229 table->setItem(row, COL_NAME, new QTableWidgetItem);
230 table->setItem(row, COL_FILE, new QTableWidgetItem);
231
232 table->setCurrentCell(row, table->currentColumn());
233}
234
235void FavoriteEditor::on_add_submenu_button_clicked() {
236 qDebug("FavoriteEditor::on_add_submenu_button_clicked");
237 qDebug("FavoriteEditor::on_add_submenu_button_clicked: store_path: '%s'", store_path.toUtf8().constData());
238
239 QString filename;
240 //QString s;
241 int n = 1;
242 do {
243 filename = QString("favorites%1.m3u8").arg(n, 4, 10, QChar('0'));
244 if (!store_path.isEmpty()) filename = store_path +"/"+ filename;
245 qDebug("FavoriteEditor::on_add_submenu_button_clicked: filename: '%s'", filename.toUtf8().constData());
246 n++;
247 } while (QFile::exists(filename));
248
249 qDebug("FavoriteEditor::on_add_submenu_button_clicked: choosen filename: '%s'", filename.toUtf8().constData());
250
251
252 int row = table->currentRow();
253 row++;
254 table->insertRow(row);
255
256 QTableWidgetItem * icon_item = new QTableWidgetItem;
257 icon_item->setData( Qt::UserRole, Images::file("openfolder.png") );
258 icon_item->setIcon( Images::icon("openfolder") );
259 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
260
261 table->setItem(row, COL_ICON, icon_item);
262 table->setItem(row, COL_NAME, new QTableWidgetItem);
263
264 QTableWidgetItem * file_item = new QTableWidgetItem;
265 file_item->setData( Qt::UserRole, true );
266 file_item->setFlags(Qt::ItemIsSelectable);
267 file_item->setData( Qt::UserRole + 1, filename );
268 file_item->setText( tr("Favorite list") );
269 file_item->setData( Qt::ToolTipRole, filename );
270 table->setItem(row, COL_FILE, file_item);
271
272 table->setCurrentCell(row, table->currentColumn());
273}
274
275void FavoriteEditor::on_up_button_clicked() {
276 int row = table->currentRow();
277 qDebug("FavoriteEditor::on_up_button_clicked: current_row: %d", row);
278
279 if (row == 0) return;
280
281 // take whole rows
282 QList<QTableWidgetItem*> source_items = takeRow(row);
283 QList<QTableWidgetItem*> dest_items = takeRow(row-1);
284
285 // set back in reverse order
286 setRow(row, dest_items);
287 setRow(row-1, source_items);
288
289 table->setCurrentCell(row-1, table->currentColumn());
290}
291
292void FavoriteEditor::on_down_button_clicked() {
293 int row = table->currentRow();
294 qDebug("FavoriteEditor::on_down_button_clicked: current_row: %d", row);
295
296 if ((row+1) >= table->rowCount()) return;
297
298 // take whole rows
299 QList<QTableWidgetItem*> source_items = takeRow(row);
300 QList<QTableWidgetItem*> dest_items = takeRow(row+1);
301
302 // set back in reverse order
303 setRow(row, dest_items);
304 setRow(row+1, source_items);
305
306 table->setCurrentCell(row+1, table->currentColumn());
307}
308
309// takes and returns the whole row
310QList<QTableWidgetItem*> FavoriteEditor::takeRow(int row) {
311 QList<QTableWidgetItem*> rowItems;
312 for (int col = 0; col < table->columnCount(); ++col)
313 {
314 rowItems << table->takeItem(row, col);
315 }
316 return rowItems;
317}
318
319// sets the whole row
320void FavoriteEditor::setRow(int row, const QList<QTableWidgetItem*>& rowItems)
321{
322 for (int col = 0; col < table->columnCount(); ++col)
323 {
324 table->setItem(row, col, rowItems.at(col));
325 }
326}
327
328void FavoriteEditor::edit_icon(int row, int column ) {
329 qDebug("FavoriteEditor::edit_icon: %d, %d", row, column);
330
331 if (column != COL_ICON) return;
332
333 QTableWidgetItem * i = table->item(row, column);
334 QString icon_filename = i->data(Qt::UserRole).toString();
335
336 qDebug("FavoriteEditor::edit_icon: icon file: '%s'", icon_filename.toUtf8().constData());
337
338 QString dir = icon_filename;
339 if (dir.isEmpty()) dir = last_dir;
340
341 QString res = QFileDialog::getOpenFileName(this, tr("Select an icon file"),
342 dir,
343 tr("Images") + " (*.png *.xpm *.jpg)");
344 if (!res.isEmpty()) {
345 i->setIcon( QIcon(res) );
346 i->setData( Qt::UserRole, res );
347
348 last_dir = QFileInfo(res).absolutePath();
349 }
350}
351
352#include "moc_favoriteeditor.cpp"
Note: See TracBrowser for help on using the repository browser.