diff options
author | Maia Kozheva <sikon@ubuntu.com> | 2012-04-09 13:30:23 +0700 |
---|---|---|
committer | Maia Kozheva <sikon@ubuntu.com> | 2012-04-09 13:30:23 +0700 |
commit | 69d32924ffbfe2bb05d881a23af13b6070b9a9bd (patch) | |
tree | 15d9f808ef672e21bcf2da9b6c9fc8e48e157eff /src/toolbareditor.cpp | |
parent | 65a64d260e05c7bf8d3bdf82e796637dc820e574 (diff) |
Imported Upstream version 0.8.0
Diffstat (limited to 'src/toolbareditor.cpp')
-rw-r--r-- | src/toolbareditor.cpp | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/toolbareditor.cpp b/src/toolbareditor.cpp index e06b95f..38562fd 100644 --- a/src/toolbareditor.cpp +++ b/src/toolbareditor.cpp @@ -20,6 +20,233 @@ #include <QToolBar> #include <QToolButton> +#include <QMatrix> + +#include "images.h" + +ToolbarEditor::ToolbarEditor( QWidget* parent, Qt::WindowFlags f ) + : QDialog(parent, f) +{ + setupUi(this); + + up_button->setIcon(Images::icon("up")); + down_button->setIcon(Images::icon("down")); + + QMatrix matrix; + matrix.rotate(90); + + right_button->setIcon( Images::icon("up").transformed(matrix) ); + left_button->setIcon( Images::icon("down").transformed(matrix) ); + + QPushButton * restore = buttonBox->button(QDialogButtonBox::RestoreDefaults); + connect(restore, SIGNAL(clicked()), this, SLOT(restoreDefaults())); + + connect(all_actions_list, SIGNAL(currentRowChanged(int)), + this, SLOT(checkRowsAllList(int))); + connect(active_actions_list, SIGNAL(currentRowChanged(int)), + this, SLOT(checkRowsActiveList(int))); + +#if QT_VERSION >= 0x040600 + all_actions_list->setSelectionMode(QAbstractItemView::SingleSelection); + all_actions_list->setDragEnabled(true); + all_actions_list->viewport()->setAcceptDrops(true); + all_actions_list->setDropIndicatorShown(true); + all_actions_list->setDefaultDropAction(Qt::MoveAction); // Qt 4.6 + //all_actions_list->setDragDropMode(QAbstractItemView::InternalMove); + + active_actions_list->setSelectionMode(QAbstractItemView::SingleSelection); + active_actions_list->setDragEnabled(true); + active_actions_list->viewport()->setAcceptDrops(true); + active_actions_list->setDropIndicatorShown(true); + active_actions_list->setDefaultDropAction(Qt::MoveAction); // Qt 4.6 + //active_actions_list->setDragDropMode(QAbstractItemView::InternalMove); +#endif +} + +ToolbarEditor::~ToolbarEditor() { +} + +void ToolbarEditor::populateList(QListWidget * w, QList<QAction *> actions_list, bool add_separators) { + w->clear(); + + QAction * action; + for (int n = 0; n < actions_list.count(); n++) { + action = static_cast<QAction*> (actions_list[n]); + if (action) { + if (!action->objectName().isEmpty()) { + QListWidgetItem * i = new QListWidgetItem; + QString text = fixname(action->text(), action->objectName()); + i->setText(text + " ("+ action->objectName() +")"); + i->setIcon(action->icon()); + i->setData(Qt::UserRole, action->objectName()); + w->addItem(i); + } + else + if ((action->isSeparator()) && (add_separators)) { + QListWidgetItem * i = new QListWidgetItem; + i->setText(tr("(separator)")); + i->setData(Qt::UserRole, "separator"); + w->addItem(i); + } + } + } +} + +void ToolbarEditor::setAllActions(QList<QAction *> actions_list) { + populateList(all_actions_list, actions_list, false); + all_actions_copy = actions_list; +} + +void ToolbarEditor::setActiveActions(QList<QAction *> actions_list) { + populateList(active_actions_list, actions_list, true); + + // Delete actions from the "all list" which are in the active list + for (int n = 0; n < active_actions_list->count(); n++) { + int row = findItem( active_actions_list->item(n)->data(Qt::UserRole).toString(), all_actions_list ); + if (row > -1) { + qDebug("found: %s", active_actions_list->item(n)->data(Qt::UserRole).toString().toUtf8().constData()); + all_actions_list->takeItem(row); + } + } +} + +int ToolbarEditor::findItem(const QString & action_name, QListWidget * w) { + for (int n = 0; n < w->count(); n++) { + if (w->item(n)->data(Qt::UserRole).toString() == action_name) { + return n; + } + } + return -1; +} + +void ToolbarEditor::on_up_button_clicked() { + int row = active_actions_list->currentRow(); + qDebug("ToolbarEditor::on_up_button_clicked: current_row: %d", row); + + if (row == 0) return; + + QListWidgetItem * current = active_actions_list->takeItem(row); + active_actions_list->insertItem(row-1, current); + active_actions_list->setCurrentRow(row-1); +} + +void ToolbarEditor::on_down_button_clicked() { + int row = active_actions_list->currentRow(); + qDebug("ToolbarEditor::on_down_button_clicked: current_row: %d", row); + + if ((row+1) >= active_actions_list->count()) return; + + QListWidgetItem * current = active_actions_list->takeItem(row); + active_actions_list->insertItem(row+1, current); + active_actions_list->setCurrentRow(row+1); +} + +void ToolbarEditor::on_right_button_clicked() { + int row = all_actions_list->currentRow(); + qDebug("ToolbarEditor::on_right_button_clicked: current_row: %d", row); + + if (row > -1) { + QListWidgetItem * current = all_actions_list->takeItem(row); + int dest_row = active_actions_list->currentRow(); + if (dest_row > -1) { + active_actions_list->insertItem(dest_row+1, current); + } else { + active_actions_list->addItem(current); + } + } +} + +void ToolbarEditor::on_left_button_clicked() { + int row = active_actions_list->currentRow(); + qDebug("ToolbarEditor::on_left_button_clicked: current_row: %d", row); + + if (row > -1) { + QListWidgetItem * current = active_actions_list->takeItem(row); + if (current->data(Qt::UserRole).toString() != "separator") { + int dest_row = all_actions_list->currentRow(); + if (dest_row > -1) { + all_actions_list->insertItem(dest_row+1, current); + } else { + all_actions_list->addItem(current); + } + } + } +} + +void ToolbarEditor::on_separator_button_clicked() { + qDebug("ToolbarEditor::on_separator_button_clicked"); + + QListWidgetItem * i = new QListWidgetItem; + i->setText(tr("(separator)")); + i->setData(Qt::UserRole, "separator"); + + int row = active_actions_list->currentRow(); + if (row > -1) { + active_actions_list->insertItem(row+1, i); + } else { + active_actions_list->addItem(i); + } +} + +void ToolbarEditor::restoreDefaults() { + qDebug("ToolbarEditor::restoreDefaults"); + populateList(all_actions_list, all_actions_copy, false); + + // Create list of actions + QList<QAction *> actions; + QAction * a = 0; + for (int n = 0; n < default_actions.count(); n++) { + if (default_actions[n] == "separator") { + QAction * sep = new QAction(this); + sep->setSeparator(true); + actions.push_back(sep); + } else { + a = findAction(default_actions[n], all_actions_copy); + if (a) actions.push_back(a); + } + } + setActiveActions(actions); +} + +QStringList ToolbarEditor::activeActionsToStringList() { + QStringList o; + for (int n = 0; n < active_actions_list->count(); n++) { + o << active_actions_list->item(n)->data(Qt::UserRole).toString(); + } + return o; +} + +void ToolbarEditor::checkRowsAllList(int currentRow) { + qDebug("ToolbarEditor::checkRowsAllList: current row: %d", currentRow); + right_button->setEnabled(currentRow > -1); +} + +void ToolbarEditor::checkRowsActiveList(int currentRow) { + qDebug("ToolbarEditor::checkRowsActiveList: current row: %d", currentRow); + left_button->setEnabled(currentRow > -1); + if (currentRow == -1) { + up_button->setEnabled(false); + down_button->setEnabled(false); + } else { + up_button->setEnabled((currentRow > 0)); + down_button->setEnabled((currentRow < active_actions_list->count()-1)); + } +} + +QString ToolbarEditor::fixname(const QString & name, const QString & action_name) { + QString s = name; + s = s.replace("&", ""); + if (action_name == "timeslider_action") s = tr("Time slider"); + else + if (action_name == "volumeslider_action") s = tr("Volume slider"); + else + if (action_name == "timelabel_action") s = tr("Display time"); + else + if (action_name == "rewindbutton_action") s = tr("3 in 1 rewind"); + else + if (action_name == "forwardbutton_action") s = tr("3 in 1 forward"); + return s; +} QStringList ToolbarEditor::save(QWidget * w) { qDebug("ToolbarEditor::save: '%s'", w->objectName().toUtf8().data()); @@ -92,3 +319,4 @@ QAction * ToolbarEditor::findAction(QString s, QList<QAction *> actions_list) { return 0; } +#include "moc_toolbareditor.cpp" |