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

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

SMPlayer: latest svn update

  • Property svn:eol-style set to LF
File size: 6.5 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2011 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
25#define COL_ICON 0
26#define COL_NAME 1
27#define COL_FILE 2
28
29FavoriteEditor::FavoriteEditor( QWidget* parent, Qt::WindowFlags f )
30 : QDialog(parent, f)
31{
32 setupUi(this);
33
34 table->setColumnCount(3);
35 table->setHorizontalHeaderLabels(QStringList() << tr("Icon") << tr("Name") << tr("Media") );
36
37 table->setAlternatingRowColors(true);
38 table->horizontalHeader()->setResizeMode(COL_FILE, QHeaderView::Stretch);
39
40 table->setSelectionBehavior(QAbstractItemView::SelectRows);
41 table->setSelectionMode(QAbstractItemView::ExtendedSelection);
42
43 connect(table, SIGNAL(cellActivated(int,int)), this, SLOT(edit_icon(int,int)));
44
45 setWindowTitle( tr("Favorite editor") );
46
47 setCaption( tr("Favorite list") );
48 setIntro( tr("You can edit, delete, sort or add new items. Double click on "
49 "a cell to edit its contents.") );
50
51 setDialogIcon( Images::icon("favorite") );
52}
53
54FavoriteEditor::~FavoriteEditor() {
55}
56
57void FavoriteEditor::setCaption(const QString & caption) {
58 caption_text = caption;
59 updateTitleLabel();
60}
61
62QString FavoriteEditor::caption() {
63 return caption_text;
64}
65
66void FavoriteEditor::setIntro(const QString & intro) {
67 intro_text = intro;
68 updateTitleLabel();
69}
70
71QString FavoriteEditor::intro() {
72 return intro_text;
73}
74
75void FavoriteEditor::updateTitleLabel() {
76 title_label->setText( "<h1>" + caption_text + "</h1>" + intro_text );
77}
78
79void FavoriteEditor::setDialogIcon( const QPixmap & icon ) {
80 dialog_icon->setPixmap(icon);
81}
82
83const QPixmap * FavoriteEditor::dialogIcon() const {
84 return dialog_icon->pixmap();
85}
86
87void FavoriteEditor::setData( FavoriteList list ) {
88 table->setRowCount(list.count());
89
90 for (int n = 0; n < list.count(); n++) {
91 QTableWidgetItem * icon_item = new QTableWidgetItem;
92 icon_item->setIcon( QIcon(list[n].icon()) );
93 icon_item->setData( Qt::UserRole, list[n].icon() );
94 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
95
96 QTableWidgetItem * name_item = new QTableWidgetItem;
97 name_item->setText( list[n].name() );
98
99 QTableWidgetItem * file_item = new QTableWidgetItem;
100 file_item->setText( list[n].file() );
101
102 table->setItem(n, COL_ICON, icon_item);
103 table->setItem(n, COL_NAME, name_item);
104 table->setItem(n, COL_FILE, file_item);
105 }
106
107 table->setCurrentCell(0, 0);
108}
109
110FavoriteList FavoriteEditor::data() {
111 FavoriteList list;
112
113 for (int n = 0; n < table->rowCount(); n++) {
114 Favorite f;
115 f.setName( table->item(n, COL_NAME)->text() );
116 f.setFile( table->item(n, COL_FILE)->text() );
117 f.setIcon( table->item(n, COL_ICON)->data(Qt::UserRole).toString() );
118
119 list.append(f);
120 }
121
122 return list;
123}
124
125void FavoriteEditor::on_delete_button_clicked() {
126 int row = table->currentRow();
127 qDebug("FavoriteEditor::on_delete_button_clicked: current_row: %d", row);
128
129 if (row > -1) table->removeRow(row);
130
131 if (row >= table->rowCount()) row--;
132 table->setCurrentCell(row, table->currentColumn());
133}
134
135void FavoriteEditor::on_delete_all_button_clicked() {
136 qDebug("FavoriteEditor::on_delete_all_button_clicked");
137 table->setRowCount(0);
138}
139
140void FavoriteEditor::on_add_button_clicked() {
141 int row = table->currentRow();
142 qDebug("FavoriteEditor::on_add_button_clicked: current_row: %d", row);
143 row++;
144 table->insertRow(row);
145
146 QTableWidgetItem * icon_item = new QTableWidgetItem;
147 icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
148
149 table->setItem(row, COL_ICON, icon_item);
150 table->setItem(row, COL_NAME, new QTableWidgetItem);
151 table->setItem(row, COL_FILE, new QTableWidgetItem);
152
153 table->setCurrentCell(row, table->currentColumn());
154}
155
156void FavoriteEditor::on_up_button_clicked() {
157 int row = table->currentRow();
158 qDebug("FavoriteEditor::on_up_button_clicked: current_row: %d", row);
159
160 if (row == 0) return;
161
162 // take whole rows
163 QList<QTableWidgetItem*> source_items = takeRow(row);
164 QList<QTableWidgetItem*> dest_items = takeRow(row-1);
165
166 // set back in reverse order
167 setRow(row, dest_items);
168 setRow(row-1, source_items);
169
170 table->setCurrentCell(row-1, table->currentColumn());
171}
172
173void FavoriteEditor::on_down_button_clicked() {
174 int row = table->currentRow();
175 qDebug("FavoriteEditor::on_down_button_clicked: current_row: %d", row);
176
177 if ((row+1) >= table->rowCount()) return;
178
179 // take whole rows
180 QList<QTableWidgetItem*> source_items = takeRow(row);
181 QList<QTableWidgetItem*> dest_items = takeRow(row+1);
182
183 // set back in reverse order
184 setRow(row, dest_items);
185 setRow(row+1, source_items);
186
187 table->setCurrentCell(row+1, table->currentColumn());
188}
189
190// takes and returns the whole row
191QList<QTableWidgetItem*> FavoriteEditor::takeRow(int row) {
192 QList<QTableWidgetItem*> rowItems;
193 for (int col = 0; col < table->columnCount(); ++col)
194 {
195 rowItems << table->takeItem(row, col);
196 }
197 return rowItems;
198}
199
200// sets the whole row
201void FavoriteEditor::setRow(int row, const QList<QTableWidgetItem*>& rowItems)
202{
203 for (int col = 0; col < table->columnCount(); ++col)
204 {
205 table->setItem(row, col, rowItems.at(col));
206 }
207}
208
209void FavoriteEditor::edit_icon(int row, int column ) {
210 qDebug("FavoriteEditor::edit_icon: %d, %d", row, column);
211
212 if (column != COL_ICON) return;
213
214 QTableWidgetItem * i = table->item(row, column);
215 QString icon_filename = i->data(Qt::UserRole).toString();
216
217 qDebug("FavoriteEditor::edit_icon: icon file: '%s'", icon_filename.toUtf8().constData());
218
219 QString dir = icon_filename;
220 if (dir.isEmpty()) dir = last_dir;
221
222 QString res = QFileDialog::getOpenFileName(this, tr("Select an icon file"),
223 dir,
224 tr("Images") + " (*.png *.xpm *.jpg)");
225 if (!res.isEmpty()) {
226 i->setIcon( QIcon(res) );
227 i->setData( Qt::UserRole, res );
228
229 last_dir = QFileInfo(res).absolutePath();
230 }
231}
232
233#include "moc_favoriteeditor.cpp"
Note: See TracBrowser for help on using the repository browser.