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