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

Last change on this file since 188 was 188, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update trunk to version 17.1.0

  • Property svn:eol-style set to LF
File size: 10.6 KB
Line 
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 "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::setIconSize(int size) {
70 iconsize_spin->setValue(size);
71}
72
73int ToolbarEditor::iconSize() {
74 return iconsize_spin->value();
75}
76
77void ToolbarEditor::populateList(QListWidget * w, QList<QAction *> actions_list, bool add_separators) {
78 w->clear();
79
80 QAction * action;
81 for (int n = 0; n < actions_list.count(); n++) {
82 action = static_cast<QAction*> (actions_list[n]);
83 if (action) {
84 if (!action->objectName().isEmpty()) {
85 QListWidgetItem * i = new QListWidgetItem;
86 QString text = fixname(action->text(), action->objectName());
87 i->setText(text + " ("+ action->objectName() +")");
88 QIcon icon = action->icon();
89 if (icon.isNull()) {
90 icon = Images::icon("empty_icon");
91 }
92 i->setIcon(icon);
93 i->setData(Qt::UserRole, action->objectName());
94 w->addItem(i);
95 }
96 else
97 if ((action->isSeparator()) && (add_separators)) {
98 QListWidgetItem * i = new QListWidgetItem;
99 //i->setText(tr("(separator)"));
100 i->setText("---------");
101 i->setData(Qt::UserRole, "separator");
102 i->setIcon(Images::icon("empty_icon"));
103 w->addItem(i);
104 }
105 }
106 }
107}
108
109void ToolbarEditor::setAllActions(QList<QAction *> actions_list) {
110 populateList(all_actions_list, actions_list, false);
111 all_actions_copy = actions_list;
112}
113
114void ToolbarEditor::setActiveActions(QList<QAction *> actions_list) {
115 populateList(active_actions_list, actions_list, true);
116
117 // Delete actions from the "all list" which are in the active list
118 for (int n = 0; n < active_actions_list->count(); n++) {
119 int row = findItem( active_actions_list->item(n)->data(Qt::UserRole).toString(), all_actions_list );
120 if (row > -1) {
121 qDebug("found: %s", active_actions_list->item(n)->data(Qt::UserRole).toString().toUtf8().constData());
122 all_actions_list->takeItem(row);
123 }
124 }
125}
126
127int ToolbarEditor::findItem(const QString & action_name, QListWidget * w) {
128 for (int n = 0; n < w->count(); n++) {
129 if (w->item(n)->data(Qt::UserRole).toString() == action_name) {
130 return n;
131 }
132 }
133 return -1;
134}
135
136void ToolbarEditor::on_up_button_clicked() {
137 int row = active_actions_list->currentRow();
138 qDebug("ToolbarEditor::on_up_button_clicked: current_row: %d", row);
139
140 if (row == 0) return;
141
142 QListWidgetItem * current = active_actions_list->takeItem(row);
143 active_actions_list->insertItem(row-1, current);
144 active_actions_list->setCurrentRow(row-1);
145}
146
147void ToolbarEditor::on_down_button_clicked() {
148 int row = active_actions_list->currentRow();
149 qDebug("ToolbarEditor::on_down_button_clicked: current_row: %d", row);
150
151 if ((row+1) >= active_actions_list->count()) return;
152
153 QListWidgetItem * current = active_actions_list->takeItem(row);
154 active_actions_list->insertItem(row+1, current);
155 active_actions_list->setCurrentRow(row+1);
156}
157
158void ToolbarEditor::on_right_button_clicked() {
159 int row = all_actions_list->currentRow();
160 qDebug("ToolbarEditor::on_right_button_clicked: current_row: %d", row);
161
162 if (row > -1) {
163 QListWidgetItem * current = all_actions_list->takeItem(row);
164 int dest_row = active_actions_list->currentRow();
165 if (dest_row > -1) {
166 active_actions_list->insertItem(dest_row+1, current);
167 } else {
168 active_actions_list->addItem(current);
169 }
170 }
171}
172
173void ToolbarEditor::on_left_button_clicked() {
174 int row = active_actions_list->currentRow();
175 qDebug("ToolbarEditor::on_left_button_clicked: current_row: %d", row);
176
177 if (row > -1) {
178 QListWidgetItem * current = active_actions_list->takeItem(row);
179 if (current->data(Qt::UserRole).toString() != "separator") {
180 int dest_row = all_actions_list->currentRow();
181 if (dest_row > -1) {
182 all_actions_list->insertItem(dest_row+1, current);
183 } else {
184 all_actions_list->addItem(current);
185 }
186 }
187 }
188}
189
190void ToolbarEditor::on_separator_button_clicked() {
191 qDebug("ToolbarEditor::on_separator_button_clicked");
192
193 QListWidgetItem * i = new QListWidgetItem;
194 //i->setText(tr("(separator)"));
195 i->setText("---------");
196 i->setData(Qt::UserRole, "separator");
197 i->setIcon(Images::icon("empty_icon"));
198
199 int row = active_actions_list->currentRow();
200 if (row > -1) {
201 active_actions_list->insertItem(row+1, i);
202 } else {
203 active_actions_list->addItem(i);
204 }
205}
206
207void ToolbarEditor::restoreDefaults() {
208 qDebug("ToolbarEditor::restoreDefaults");
209 populateList(all_actions_list, all_actions_copy, false);
210
211 // Create list of actions
212 QList<QAction *> actions;
213 QAction * a = 0;
214 for (int n = 0; n < default_actions.count(); n++) {
215 if (default_actions[n] == "separator") {
216 QAction * sep = new QAction(this);
217 sep->setSeparator(true);
218 actions.push_back(sep);
219 } else {
220 a = findAction(default_actions[n], all_actions_copy);
221 if (a) actions.push_back(a);
222 }
223 }
224 setActiveActions(actions);
225}
226
227QStringList ToolbarEditor::activeActionsToStringList() {
228 QStringList o;
229 for (int n = 0; n < active_actions_list->count(); n++) {
230 o << active_actions_list->item(n)->data(Qt::UserRole).toString();
231 }
232 return o;
233}
234
235void ToolbarEditor::checkRowsAllList(int currentRow) {
236 qDebug("ToolbarEditor::checkRowsAllList: current row: %d", currentRow);
237 right_button->setEnabled(currentRow > -1);
238}
239
240void ToolbarEditor::checkRowsActiveList(int currentRow) {
241 qDebug("ToolbarEditor::checkRowsActiveList: current row: %d", currentRow);
242 left_button->setEnabled(currentRow > -1);
243 if (currentRow == -1) {
244 up_button->setEnabled(false);
245 down_button->setEnabled(false);
246 } else {
247 up_button->setEnabled((currentRow > 0));
248 down_button->setEnabled((currentRow < active_actions_list->count()-1));
249 }
250}
251
252QString ToolbarEditor::fixname(const QString & name, const QString & action_name) {
253 QString s = name;
254 s = s.replace("&", "");
255 if (action_name == "timeslider_action") s = tr("Time slider");
256 else
257 if (action_name == "volumeslider_action") s = tr("Volume slider");
258 else
259 if (action_name == "timelabel_action") s = tr("Display time");
260 else
261 if (action_name == "current_timelabel_action") s = tr("Current time");
262 else
263 if (action_name == "total_timelabel_action") s = tr("Total time");
264 else
265 if (action_name == "remaining_timelabel_action") s = tr("Remaining time");
266 else
267 if (action_name == "rewindbutton_action") s = tr("3 in 1 rewind");
268 else
269 if (action_name == "forwardbutton_action") s = tr("3 in 1 forward");
270 else
271 if (action_name == "quick_access_menu") s = tr("Quick access menu");
272 return s;
273}
274
275QStringList ToolbarEditor::save(QWidget * w) {
276 qDebug("ToolbarEditor::save: '%s'", w->objectName().toUtf8().data());
277
278 QList<QAction *> list = w->actions();
279 QStringList o;
280 QAction * action;
281
282 for (int n = 0; n < list.count(); n++) {
283 action = static_cast<QAction*> (list[n]);
284 if (action->isSeparator()) {
285 o << "separator";
286 }
287 else
288 if (!action->objectName().isEmpty()) {
289 o << action->objectName();
290 }
291 else
292 qWarning("ToolbarEditor::save: unknown action at pos %d", n);
293 }
294
295 return o;
296}
297
298void ToolbarEditor::load(QWidget *w, QStringList l, QList<QAction *> actions_list)
299{
300 qDebug("ToolbarEditor::load: '%s'", w->objectName().toUtf8().data());
301
302 QAction * action;
303
304 for (int n = 0; n < l.count(); n++) {
305 qDebug("ToolbarEditor::load: loading action %s", l[n].toUtf8().data());
306
307 if (l[n] == "separator") {
308 qDebug("ToolbarEditor::load: adding separator");
309 QAction * sep = new QAction(w);
310 sep->setSeparator(true);
311 w->addAction(sep);
312 } else {
313 action = findAction(l[n], actions_list);
314 if (action) {
315 w->addAction(action);
316 if (action->objectName().endsWith("_menu")) {
317 // If the action is a menu and is in a toolbar, as a toolbutton, change some of its properties
318 QToolBar * toolbar = qobject_cast<QToolBar *>(w);
319 if (toolbar) {
320 QToolButton * button = qobject_cast<QToolButton *>(toolbar->widgetForAction(action));
321 if (button) {
322 //qDebug("ToolbarEditor::load: action %s is a toolbutton", action->objectName().toUtf8().constData());
323 button->setPopupMode(QToolButton::InstantPopup);
324 //button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
325 }
326 }
327 }
328 } else {
329 qWarning("ToolbarEditor::load: action %s not found", l[n].toUtf8().data());
330 }
331 }
332 }
333}
334
335QAction * ToolbarEditor::findAction(QString s, QList<QAction *> actions_list) {
336 QAction * action;
337
338 for (int n = 0; n < actions_list.count(); n++) {
339 action = static_cast<QAction*> (actions_list[n]);
340 if (action->objectName() == s) return action;
341 }
342
343 return 0;
344}
345
346#include "moc_toolbareditor.cpp"
Note: See TracBrowser for help on using the repository browser.