| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2016 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 |
|
|---|
| 27 | ToolbarEditor::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 |
|
|---|
| 66 | ToolbarEditor::~ToolbarEditor() {
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | void ToolbarEditor::setIconSize(int size) {
|
|---|
| 70 | iconsize_spin->setValue(size);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | int ToolbarEditor::iconSize() {
|
|---|
| 74 | return iconsize_spin->value();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void 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 |
|
|---|
| 109 | void ToolbarEditor::setAllActions(QList<QAction *> actions_list) {
|
|---|
| 110 | populateList(all_actions_list, actions_list, false);
|
|---|
| 111 | all_actions_copy = actions_list;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void 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 |
|
|---|
| 127 | int 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 |
|
|---|
| 136 | void 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 |
|
|---|
| 147 | void 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 |
|
|---|
| 158 | void 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 |
|
|---|
| 173 | void 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 |
|
|---|
| 190 | void 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 |
|
|---|
| 207 | void 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 |
|
|---|
| 227 | QStringList 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 |
|
|---|
| 235 | void ToolbarEditor::checkRowsAllList(int currentRow) {
|
|---|
| 236 | qDebug("ToolbarEditor::checkRowsAllList: current row: %d", currentRow);
|
|---|
| 237 | right_button->setEnabled(currentRow > -1);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | void 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 |
|
|---|
| 252 | QString 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 == "rewindbutton_action") s = tr("3 in 1 rewind");
|
|---|
| 262 | else
|
|---|
| 263 | if (action_name == "forwardbutton_action") s = tr("3 in 1 forward");
|
|---|
| 264 | return s;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | QStringList ToolbarEditor::save(QWidget * w) {
|
|---|
| 268 | qDebug("ToolbarEditor::save: '%s'", w->objectName().toUtf8().data());
|
|---|
| 269 |
|
|---|
| 270 | QList<QAction *> list = w->actions();
|
|---|
| 271 | QStringList o;
|
|---|
| 272 | QAction * action;
|
|---|
| 273 |
|
|---|
| 274 | for (int n = 0; n < list.count(); n++) {
|
|---|
| 275 | action = static_cast<QAction*> (list[n]);
|
|---|
| 276 | if (action->isSeparator()) {
|
|---|
| 277 | o << "separator";
|
|---|
| 278 | }
|
|---|
| 279 | else
|
|---|
| 280 | if (!action->objectName().isEmpty()) {
|
|---|
| 281 | o << action->objectName();
|
|---|
| 282 | }
|
|---|
| 283 | else
|
|---|
| 284 | qWarning("ToolbarEditor::save: unknown action at pos %d", n);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | return o;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | void ToolbarEditor::load(QWidget *w, QStringList l, QList<QAction *> actions_list)
|
|---|
| 291 | {
|
|---|
| 292 | qDebug("ToolbarEditor::load: '%s'", w->objectName().toUtf8().data());
|
|---|
| 293 |
|
|---|
| 294 | QAction * action;
|
|---|
| 295 |
|
|---|
| 296 | for (int n = 0; n < l.count(); n++) {
|
|---|
| 297 | qDebug("ToolbarEditor::load: loading action %s", l[n].toUtf8().data());
|
|---|
| 298 |
|
|---|
| 299 | if (l[n] == "separator") {
|
|---|
| 300 | qDebug("ToolbarEditor::load: adding separator");
|
|---|
| 301 | QAction * sep = new QAction(w);
|
|---|
| 302 | sep->setSeparator(true);
|
|---|
| 303 | w->addAction(sep);
|
|---|
| 304 | } else {
|
|---|
| 305 | action = findAction(l[n], actions_list);
|
|---|
| 306 | if (action) {
|
|---|
| 307 | w->addAction(action);
|
|---|
| 308 | if (action->objectName().endsWith("_menu")) {
|
|---|
| 309 | // If the action is a menu and is in a toolbar, as a toolbutton, change some of its properties
|
|---|
| 310 | QToolBar * toolbar = qobject_cast<QToolBar *>(w);
|
|---|
| 311 | if (toolbar) {
|
|---|
| 312 | QToolButton * button = qobject_cast<QToolButton *>(toolbar->widgetForAction(action));
|
|---|
| 313 | if (button) {
|
|---|
| 314 | //qDebug("ToolbarEditor::load: action %s is a toolbutton", action->objectName().toUtf8().constData());
|
|---|
| 315 | button->setPopupMode(QToolButton::InstantPopup);
|
|---|
| 316 | //button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 | } else {
|
|---|
| 321 | qWarning("ToolbarEditor::load: action %s not found", l[n].toUtf8().data());
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | QAction * ToolbarEditor::findAction(QString s, QList<QAction *> actions_list) {
|
|---|
| 328 | QAction * action;
|
|---|
| 329 |
|
|---|
| 330 | for (int n = 0; n < actions_list.count(); n++) {
|
|---|
| 331 | action = static_cast<QAction*> (actions_list[n]);
|
|---|
| 332 | if (action->objectName() == s) return action;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | return 0;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | #include "moc_toolbareditor.cpp"
|
|---|