source: smplayer/trunk/src/toolbareditor.cpp@ 175

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

SMPlayer: update trunk to latest 0.8.7

  • Property svn:eol-style set to LF
File size: 9.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 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 "toolbareditor.h"
20
21#include <QToolBar>
22#include <QToolButton>
23#include <QMatrix>
24
25#include "images.h"
26
27ToolbarEditor::ToolbarEditor( QWidget* parent, Qt::WindowFlags f )
28 : QDialog(parent, f)
29{
30 setupUi(this);
31
32 up_button->setIcon(Images::icon("up"));
33 down_button->setIcon(Images::icon("down"));
34
35 QMatrix matrix;
36 matrix.rotate(90);
37
38 right_button->setIcon( Images::icon("up").transformed(matrix) );
39 left_button->setIcon( Images::icon("down").transformed(matrix) );
40
41 QPushButton * restore = buttonBox->button(QDialogButtonBox::RestoreDefaults);
42 connect(restore, SIGNAL(clicked()), this, SLOT(restoreDefaults()));
43
44 connect(all_actions_list, SIGNAL(currentRowChanged(int)),
45 this, SLOT(checkRowsAllList(int)));
46 connect(active_actions_list, SIGNAL(currentRowChanged(int)),
47 this, SLOT(checkRowsActiveList(int)));
48
49#if QT_VERSION >= 0x040600
50 all_actions_list->setSelectionMode(QAbstractItemView::SingleSelection);
51 all_actions_list->setDragEnabled(true);
52 all_actions_list->viewport()->setAcceptDrops(true);
53 all_actions_list->setDropIndicatorShown(true);
54 all_actions_list->setDefaultDropAction(Qt::MoveAction); // Qt 4.6
55 //all_actions_list->setDragDropMode(QAbstractItemView::InternalMove);
56
57 active_actions_list->setSelectionMode(QAbstractItemView::SingleSelection);
58 active_actions_list->setDragEnabled(true);
59 active_actions_list->viewport()->setAcceptDrops(true);
60 active_actions_list->setDropIndicatorShown(true);
61 active_actions_list->setDefaultDropAction(Qt::MoveAction); // Qt 4.6
62 //active_actions_list->setDragDropMode(QAbstractItemView::InternalMove);
63#endif
64}
65
66ToolbarEditor::~ToolbarEditor() {
67}
68
69void ToolbarEditor::populateList(QListWidget * w, QList<QAction *> actions_list, bool add_separators) {
70 w->clear();
71
72 QAction * action;
73 for (int n = 0; n < actions_list.count(); n++) {
74 action = static_cast<QAction*> (actions_list[n]);
75 if (action) {
76 if (!action->objectName().isEmpty()) {
77 QListWidgetItem * i = new QListWidgetItem;
78 QString text = fixname(action->text(), action->objectName());
79 i->setText(text + " ("+ action->objectName() +")");
80 i->setIcon(action->icon());
81 i->setData(Qt::UserRole, action->objectName());
82 w->addItem(i);
83 }
84 else
85 if ((action->isSeparator()) && (add_separators)) {
86 QListWidgetItem * i = new QListWidgetItem;
87 i->setText(tr("(separator)"));
88 i->setData(Qt::UserRole, "separator");
89 w->addItem(i);
90 }
91 }
92 }
93}
94
95void ToolbarEditor::setAllActions(QList<QAction *> actions_list) {
96 populateList(all_actions_list, actions_list, false);
97 all_actions_copy = actions_list;
98}
99
100void ToolbarEditor::setActiveActions(QList<QAction *> actions_list) {
101 populateList(active_actions_list, actions_list, true);
102
103 // Delete actions from the "all list" which are in the active list
104 for (int n = 0; n < active_actions_list->count(); n++) {
105 int row = findItem( active_actions_list->item(n)->data(Qt::UserRole).toString(), all_actions_list );
106 if (row > -1) {
107 qDebug("found: %s", active_actions_list->item(n)->data(Qt::UserRole).toString().toUtf8().constData());
108 all_actions_list->takeItem(row);
109 }
110 }
111}
112
113int ToolbarEditor::findItem(const QString & action_name, QListWidget * w) {
114 for (int n = 0; n < w->count(); n++) {
115 if (w->item(n)->data(Qt::UserRole).toString() == action_name) {
116 return n;
117 }
118 }
119 return -1;
120}
121
122void ToolbarEditor::on_up_button_clicked() {
123 int row = active_actions_list->currentRow();
124 qDebug("ToolbarEditor::on_up_button_clicked: current_row: %d", row);
125
126 if (row == 0) return;
127
128 QListWidgetItem * current = active_actions_list->takeItem(row);
129 active_actions_list->insertItem(row-1, current);
130 active_actions_list->setCurrentRow(row-1);
131}
132
133void ToolbarEditor::on_down_button_clicked() {
134 int row = active_actions_list->currentRow();
135 qDebug("ToolbarEditor::on_down_button_clicked: current_row: %d", row);
136
137 if ((row+1) >= active_actions_list->count()) return;
138
139 QListWidgetItem * current = active_actions_list->takeItem(row);
140 active_actions_list->insertItem(row+1, current);
141 active_actions_list->setCurrentRow(row+1);
142}
143
144void ToolbarEditor::on_right_button_clicked() {
145 int row = all_actions_list->currentRow();
146 qDebug("ToolbarEditor::on_right_button_clicked: current_row: %d", row);
147
148 if (row > -1) {
149 QListWidgetItem * current = all_actions_list->takeItem(row);
150 int dest_row = active_actions_list->currentRow();
151 if (dest_row > -1) {
152 active_actions_list->insertItem(dest_row+1, current);
153 } else {
154 active_actions_list->addItem(current);
155 }
156 }
157}
158
159void ToolbarEditor::on_left_button_clicked() {
160 int row = active_actions_list->currentRow();
161 qDebug("ToolbarEditor::on_left_button_clicked: current_row: %d", row);
162
163 if (row > -1) {
164 QListWidgetItem * current = active_actions_list->takeItem(row);
165 if (current->data(Qt::UserRole).toString() != "separator") {
166 int dest_row = all_actions_list->currentRow();
167 if (dest_row > -1) {
168 all_actions_list->insertItem(dest_row+1, current);
169 } else {
170 all_actions_list->addItem(current);
171 }
172 }
173 }
174}
175
176void ToolbarEditor::on_separator_button_clicked() {
177 qDebug("ToolbarEditor::on_separator_button_clicked");
178
179 QListWidgetItem * i = new QListWidgetItem;
180 i->setText(tr("(separator)"));
181 i->setData(Qt::UserRole, "separator");
182
183 int row = active_actions_list->currentRow();
184 if (row > -1) {
185 active_actions_list->insertItem(row+1, i);
186 } else {
187 active_actions_list->addItem(i);
188 }
189}
190
191void ToolbarEditor::restoreDefaults() {
192 qDebug("ToolbarEditor::restoreDefaults");
193 populateList(all_actions_list, all_actions_copy, false);
194
195 // Create list of actions
196 QList<QAction *> actions;
197 QAction * a = 0;
198 for (int n = 0; n < default_actions.count(); n++) {
199 if (default_actions[n] == "separator") {
200 QAction * sep = new QAction(this);
201 sep->setSeparator(true);
202 actions.push_back(sep);
203 } else {
204 a = findAction(default_actions[n], all_actions_copy);
205 if (a) actions.push_back(a);
206 }
207 }
208 setActiveActions(actions);
209}
210
211QStringList ToolbarEditor::activeActionsToStringList() {
212 QStringList o;
213 for (int n = 0; n < active_actions_list->count(); n++) {
214 o << active_actions_list->item(n)->data(Qt::UserRole).toString();
215 }
216 return o;
217}
218
219void ToolbarEditor::checkRowsAllList(int currentRow) {
220 qDebug("ToolbarEditor::checkRowsAllList: current row: %d", currentRow);
221 right_button->setEnabled(currentRow > -1);
222}
223
224void ToolbarEditor::checkRowsActiveList(int currentRow) {
225 qDebug("ToolbarEditor::checkRowsActiveList: current row: %d", currentRow);
226 left_button->setEnabled(currentRow > -1);
227 if (currentRow == -1) {
228 up_button->setEnabled(false);
229 down_button->setEnabled(false);
230 } else {
231 up_button->setEnabled((currentRow > 0));
232 down_button->setEnabled((currentRow < active_actions_list->count()-1));
233 }
234}
235
236QString ToolbarEditor::fixname(const QString & name, const QString & action_name) {
237 QString s = name;
238 s = s.replace("&", "");
239 if (action_name == "timeslider_action") s = tr("Time slider");
240 else
241 if (action_name == "volumeslider_action") s = tr("Volume slider");
242 else
243 if (action_name == "timelabel_action") s = tr("Display time");
244 else
245 if (action_name == "rewindbutton_action") s = tr("3 in 1 rewind");
246 else
247 if (action_name == "forwardbutton_action") s = tr("3 in 1 forward");
248 return s;
249}
250
251QStringList ToolbarEditor::save(QWidget * w) {
252 qDebug("ToolbarEditor::save: '%s'", w->objectName().toUtf8().data());
253
254 QList<QAction *> list = w->actions();
255 QStringList o;
256 QAction * action;
257
258 for (int n = 0; n < list.count(); n++) {
259 action = static_cast<QAction*> (list[n]);
260 if (action->isSeparator()) {
261 o << "separator";
262 }
263 else
264 if (!action->objectName().isEmpty()) {
265 o << action->objectName();
266 }
267 else
268 qWarning("ToolbarEditor::save: unknown action at pos %d", n);
269 }
270
271 return o;
272}
273
274void ToolbarEditor::load(QWidget *w, QStringList l, QList<QAction *> actions_list)
275{
276 qDebug("ToolbarEditor::load: '%s'", w->objectName().toUtf8().data());
277
278 QAction * action;
279
280 for (int n = 0; n < l.count(); n++) {
281 qDebug("ToolbarEditor::load: loading action %s", l[n].toUtf8().data());
282
283 if (l[n] == "separator") {
284 qDebug("ToolbarEditor::load: adding separator");
285 QAction * sep = new QAction(w);
286 sep->setSeparator(true);
287 w->addAction(sep);
288 } else {
289 action = findAction(l[n], actions_list);
290 if (action) {
291 w->addAction(action);
292 if (action->objectName().endsWith("_menu")) {
293 // If the action is a menu and is in a toolbar, as a toolbutton, change some of its properties
294 QToolBar * toolbar = qobject_cast<QToolBar *>(w);
295 if (toolbar) {
296 QToolButton * button = qobject_cast<QToolButton *>(toolbar->widgetForAction(action));
297 if (button) {
298 //qDebug("ToolbarEditor::load: action %s is a toolbutton", action->objectName().toUtf8().constData());
299 button->setPopupMode(QToolButton::InstantPopup);
300 //button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
301 }
302 }
303 }
304 } else {
305 qWarning("ToolbarEditor::load: action %s not found", l[n].toUtf8().data());
306 }
307 }
308 }
309}
310
311QAction * ToolbarEditor::findAction(QString s, QList<QAction *> actions_list) {
312 QAction * action;
313
314 for (int n = 0; n < actions_list.count(); n++) {
315 action = static_cast<QAction*> (actions_list[n]);
316 if (action->objectName() == s) return action;
317 }
318
319 return 0;
320}
321
322#include "moc_toolbareditor.cpp"
Note: See TracBrowser for help on using the repository browser.