source: smplayer/vendor/current/src/favoriteeditor.cpp@ 175

Last change on this file since 175 was 175, checked in by Silvan Scherrer, 10 years ago

smplayer: update vendor to version 16.4

File size: 10.7 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2016 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#if QT_VERSION >= 0x050000
98 table->horizontalHeader()->setSectionResizeMode(COL_FILE, QHeaderView::Stretch);
99#else
100 table->horizontalHeader()->setResizeMode(COL_FILE, QHeaderView::Stretch);
101#endif
102
103 table->setSelectionBehavior(QAbstractItemView::SelectRows);
104 table->setSelectionMode(QAbstractItemView::ExtendedSelection);
105
106 table->setItemDelegateForColumn( COL_NAME, new FEDelegate(table) );
107 table->setItemDelegateForColumn( COL_FILE, new FEDelegate(table) );
108
109 connect(table, SIGNAL(cellActivated(int,int)), this, SLOT(edit_icon(int,int)));
110
111 setWindowTitle( tr("Favorite editor") );
112
113 setCaption( tr("Favorite list") );
114 setIntro( tr("You can edit, delete, sort or add new items. Double click on "
115 "a cell to edit its contents.") );
116
117 setDialogIcon( Images::icon("favorite") );
118}
119
120FavoriteEditor::~FavoriteEditor() {
121}
122
123void FavoriteEditor::setCaption(const QString & caption) {
124 caption_text = caption;
125 updateTitleLabel();
126}
127
128QString FavoriteEditor::caption() {
129 return caption_text;
130}
131
132void FavoriteEditor::setIntro(const QString & intro) {
133 intro_text = intro;
134 updateTitleLabel();
135}
136
137QString FavoriteEditor::intro() {
138 return intro_text;
139}
140
141void FavoriteEditor::updateTitleLabel() {
142 title_label->setText( "<h1>" + caption_text + "</h1>" + intro_text );
143}
144
145void FavoriteEditor::setDialogIcon( const QPixmap & icon ) {
146 dialog_icon->setPixmap(icon);
147}
148
149const QPixmap * FavoriteEditor::dialogIcon() const {
150 return dialog_icon->pixmap();
151}
152
153void FavoriteEditor::setData( FavoriteList list ) {
154 table->setRowCount(list.count());
155
156 for (int n = 0; n < list.count(); n++) {
157 QTableWidgetItem * icon_item = new QTableWidgetItem;
158 icon_item->setIcon( QIcon(list[n].icon()) );
159 icon_item->setData( Qt::UserRole, list[n].icon() );
160 icon_item->setData( Qt::ToolTipRole, list[n].icon() );
161 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
162
163 QTableWidgetItem * name_item = new QTableWidgetItem;
164 name_item->setText( list[n].name() );
165
166 QTableWidgetItem * file_item = new QTableWidgetItem;
167 file_item->setData( Qt::ToolTipRole, list[n].file() );
168 file_item->setData( Qt::UserRole, list[n].isSubentry() );
169 if (list[n].isSubentry()) {
170 file_item->setFlags(Qt::ItemIsSelectable);
171 file_item->setData( Qt::UserRole + 1, list[n].file() );
172 file_item->setText( tr("Favorite list") );
173 } else {
174 file_item->setText( list[n].file() );
175 }
176
177 table->setItem(n, COL_ICON, icon_item);
178 table->setItem(n, COL_NAME, name_item);
179 table->setItem(n, COL_FILE, file_item);
180 }
181
182 //table->resizeColumnsToContents();
183
184 //table->setCurrentCell(0, 0);
185 table->setCurrentCell(table->rowCount()-1, 0);
186}
187
188FavoriteList FavoriteEditor::data() {
189 FavoriteList list;
190
191 for (int n = 0; n < table->rowCount(); n++) {
192 Favorite f;
193 f.setName( table->item(n, COL_NAME)->text() );
194 f.setIcon( table->item(n, COL_ICON)->data(Qt::UserRole).toString() );
195 f.setSubentry( table->item(n, COL_FILE)->data(Qt::UserRole).toBool() );
196 if (f.isSubentry()) {
197 f.setFile( table->item(n, COL_FILE)->data(Qt::UserRole + 1).toString() );
198 } else {
199 f.setFile( table->item(n, COL_FILE)->text() );
200 }
201
202 list.append(f);
203 }
204
205 return list;
206}
207
208void FavoriteEditor::on_delete_button_clicked() {
209 int row = table->currentRow();
210 qDebug("FavoriteEditor::on_delete_button_clicked: current_row: %d", row);
211
212 if (row > -1) table->removeRow(row);
213
214 if (row >= table->rowCount()) row--;
215 table->setCurrentCell(row, table->currentColumn());
216}
217
218void FavoriteEditor::on_delete_all_button_clicked() {
219 qDebug("FavoriteEditor::on_delete_all_button_clicked");
220 table->setRowCount(0);
221}
222
223void FavoriteEditor::on_add_button_clicked() {
224 int row = table->currentRow();
225 qDebug("FavoriteEditor::on_add_button_clicked: current_row: %d", row);
226 row++;
227 table->insertRow(row);
228
229 QTableWidgetItem * icon_item = new QTableWidgetItem;
230 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
231
232 table->setItem(row, COL_ICON, icon_item);
233 table->setItem(row, COL_NAME, new QTableWidgetItem);
234 table->setItem(row, COL_FILE, new QTableWidgetItem);
235
236 table->setCurrentCell(row, table->currentColumn());
237}
238
239void FavoriteEditor::on_add_submenu_button_clicked() {
240 qDebug("FavoriteEditor::on_add_submenu_button_clicked");
241 qDebug("FavoriteEditor::on_add_submenu_button_clicked: store_path: '%s'", store_path.toUtf8().constData());
242
243 QString filename;
244 //QString s;
245 int n = 1;
246 do {
247 filename = QString("favorites%1.m3u8").arg(n, 4, 10, QChar('0'));
248 if (!store_path.isEmpty()) filename = store_path +"/"+ filename;
249 qDebug("FavoriteEditor::on_add_submenu_button_clicked: filename: '%s'", filename.toUtf8().constData());
250 n++;
251 } while (QFile::exists(filename));
252
253 qDebug("FavoriteEditor::on_add_submenu_button_clicked: chosen filename: '%s'", filename.toUtf8().constData());
254
255
256 int row = table->currentRow();
257 row++;
258 table->insertRow(row);
259
260 QTableWidgetItem * icon_item = new QTableWidgetItem;
261 icon_item->setData( Qt::UserRole, Images::file("openfolder") );
262 icon_item->setIcon( Images::icon("openfolder") );
263 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
264
265 table->setItem(row, COL_ICON, icon_item);
266 table->setItem(row, COL_NAME, new QTableWidgetItem);
267
268 QTableWidgetItem * file_item = new QTableWidgetItem;
269 file_item->setData( Qt::UserRole, true );
270 file_item->setFlags(Qt::ItemIsSelectable);
271 file_item->setData( Qt::UserRole + 1, filename );
272 file_item->setText( tr("Favorite list") );
273 file_item->setData( Qt::ToolTipRole, filename );
274 table->setItem(row, COL_FILE, file_item);
275
276 table->setCurrentCell(row, table->currentColumn());
277}
278
279void FavoriteEditor::on_up_button_clicked() {
280 int row = table->currentRow();
281 qDebug("FavoriteEditor::on_up_button_clicked: current_row: %d", row);
282
283 if (row == 0) return;
284
285 // take whole rows
286 QList<QTableWidgetItem*> source_items = takeRow(row);
287 QList<QTableWidgetItem*> dest_items = takeRow(row-1);
288
289 // set back in reverse order
290 setRow(row, dest_items);
291 setRow(row-1, source_items);
292
293 table->setCurrentCell(row-1, table->currentColumn());
294}
295
296void FavoriteEditor::on_down_button_clicked() {
297 int row = table->currentRow();
298 qDebug("FavoriteEditor::on_down_button_clicked: current_row: %d", row);
299
300 if ((row+1) >= table->rowCount()) return;
301
302 // take whole rows
303 QList<QTableWidgetItem*> source_items = takeRow(row);
304 QList<QTableWidgetItem*> dest_items = takeRow(row+1);
305
306 // set back in reverse order
307 setRow(row, dest_items);
308 setRow(row+1, source_items);
309
310 table->setCurrentCell(row+1, table->currentColumn());
311}
312
313// takes and returns the whole row
314QList<QTableWidgetItem*> FavoriteEditor::takeRow(int row) {
315 QList<QTableWidgetItem*> rowItems;
316 for (int col = 0; col < table->columnCount(); ++col)
317 {
318 rowItems << table->takeItem(row, col);
319 }
320 return rowItems;
321}
322
323// sets the whole row
324void FavoriteEditor::setRow(int row, const QList<QTableWidgetItem*>& rowItems)
325{
326 for (int col = 0; col < table->columnCount(); ++col)
327 {
328 table->setItem(row, col, rowItems.at(col));
329 }
330}
331
332void FavoriteEditor::edit_icon(int row, int column ) {
333 qDebug("FavoriteEditor::edit_icon: %d, %d", row, column);
334
335 if (column != COL_ICON) return;
336
337 QTableWidgetItem * i = table->item(row, column);
338 QString icon_filename = i->data(Qt::UserRole).toString();
339
340 qDebug("FavoriteEditor::edit_icon: icon file: '%s'", icon_filename.toUtf8().constData());
341
342 QString dir = icon_filename;
343 if (dir.isEmpty()) dir = last_dir;
344
345 QString res = QFileDialog::getOpenFileName(this, tr("Select an icon file"),
346 dir,
347 tr("Images") + " (*.png *.xpm *.jpg)");
348 if (!res.isEmpty()) {
349 i->setIcon( QIcon(res) );
350 i->setData( Qt::UserRole, res );
351
352 last_dir = QFileInfo(res).absolutePath();
353 }
354}
355
356#include "moc_favoriteeditor.cpp"
Note: See TracBrowser for help on using the repository browser.