| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2012 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 | #include <QItemDelegate>
|
|---|
| 25 | #include "filechooser.h"
|
|---|
| 26 |
|
|---|
| 27 | #define COL_ICON 0
|
|---|
| 28 | #define COL_NAME 1
|
|---|
| 29 | #define COL_FILE 2
|
|---|
| 30 |
|
|---|
| 31 | #include <QItemDelegate>
|
|---|
| 32 |
|
|---|
| 33 | class FEDelegate : public QItemDelegate
|
|---|
| 34 | {
|
|---|
| 35 | public:
|
|---|
| 36 | FEDelegate(QObject *parent = 0);
|
|---|
| 37 |
|
|---|
| 38 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
|---|
| 39 | const QModelIndex &index) const;
|
|---|
| 40 | virtual void setModelData(QWidget * editor, QAbstractItemModel * model,
|
|---|
| 41 | const QModelIndex & index ) const;
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | FEDelegate::FEDelegate(QObject *parent) : QItemDelegate(parent) {
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | QWidget * FEDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
|
|---|
| 48 | //qDebug("FEDelegate::createEditor");
|
|---|
| 49 |
|
|---|
| 50 | if (index.column() == COL_FILE) {
|
|---|
| 51 | FileChooser * fch = new FileChooser(parent);
|
|---|
| 52 | fch->setOptions(QFileDialog::DontUseNativeDialog | QFileDialog::DontResolveSymlinks); // Crashes if use the KDE dialog
|
|---|
| 53 | fch->setText( index.model()->data(index, Qt::DisplayRole).toString() );
|
|---|
| 54 | return fch;
|
|---|
| 55 | }
|
|---|
| 56 | else
|
|---|
| 57 | if (index.column() == COL_NAME) {
|
|---|
| 58 | QLineEdit * e = new QLineEdit(parent);
|
|---|
| 59 | e->setText( index.model()->data(index, Qt::DisplayRole).toString() );
|
|---|
| 60 | return e;
|
|---|
| 61 | }
|
|---|
| 62 | else {
|
|---|
| 63 | return QItemDelegate::createEditor(parent, option, index);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void FEDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
|
|---|
| 68 | if (index.column() == COL_FILE) {
|
|---|
| 69 | FileChooser * fch = static_cast<FileChooser*>(editor);
|
|---|
| 70 | model->setData(index, fch->text() );
|
|---|
| 71 | }
|
|---|
| 72 | else
|
|---|
| 73 | if (index.column() == COL_NAME) {
|
|---|
| 74 | QLineEdit * e = static_cast<QLineEdit*>(editor);
|
|---|
| 75 | model->setData(index, e->text() );
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | QString FavoriteEditor::last_dir;
|
|---|
| 80 |
|
|---|
| 81 | FavoriteEditor::FavoriteEditor( QWidget* parent, Qt::WindowFlags f )
|
|---|
| 82 | : QDialog(parent, f)
|
|---|
| 83 | {
|
|---|
| 84 | setupUi(this);
|
|---|
| 85 |
|
|---|
| 86 | add_button->setIcon( Images::icon("bookmark_add") );
|
|---|
| 87 | add_submenu_button->setIcon( Images::icon("bookmark_folder") );
|
|---|
| 88 | delete_button->setIcon( Images::icon("delete") );
|
|---|
| 89 | delete_all_button->setIcon( Images::icon("trash") );
|
|---|
| 90 | up_button->setIcon( Images::icon("up") );
|
|---|
| 91 | down_button->setIcon( Images::icon("down") );
|
|---|
| 92 |
|
|---|
| 93 | table->setColumnCount(3);
|
|---|
| 94 | table->setHorizontalHeaderLabels(QStringList() << tr("Icon") << tr("Name") << tr("Media") );
|
|---|
| 95 |
|
|---|
| 96 | table->setAlternatingRowColors(true);
|
|---|
| 97 | table->horizontalHeader()->setResizeMode(COL_FILE, QHeaderView::Stretch);
|
|---|
| 98 |
|
|---|
| 99 | table->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|---|
| 100 | table->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|---|
| 101 |
|
|---|
| 102 | table->setItemDelegateForColumn( COL_NAME, new FEDelegate(table) );
|
|---|
| 103 | table->setItemDelegateForColumn( COL_FILE, new FEDelegate(table) );
|
|---|
| 104 |
|
|---|
| 105 | connect(table, SIGNAL(cellActivated(int,int)), this, SLOT(edit_icon(int,int)));
|
|---|
| 106 |
|
|---|
| 107 | setWindowTitle( tr("Favorite editor") );
|
|---|
| 108 |
|
|---|
| 109 | setCaption( tr("Favorite list") );
|
|---|
| 110 | setIntro( tr("You can edit, delete, sort or add new items. Double click on "
|
|---|
| 111 | "a cell to edit its contents.") );
|
|---|
| 112 |
|
|---|
| 113 | setDialogIcon( Images::icon("favorite") );
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | FavoriteEditor::~FavoriteEditor() {
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void FavoriteEditor::setCaption(const QString & caption) {
|
|---|
| 120 | caption_text = caption;
|
|---|
| 121 | updateTitleLabel();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | QString FavoriteEditor::caption() {
|
|---|
| 125 | return caption_text;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | void FavoriteEditor::setIntro(const QString & intro) {
|
|---|
| 129 | intro_text = intro;
|
|---|
| 130 | updateTitleLabel();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | QString FavoriteEditor::intro() {
|
|---|
| 134 | return intro_text;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void FavoriteEditor::updateTitleLabel() {
|
|---|
| 138 | title_label->setText( "<h1>" + caption_text + "</h1>" + intro_text );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void FavoriteEditor::setDialogIcon( const QPixmap & icon ) {
|
|---|
| 142 | dialog_icon->setPixmap(icon);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | const QPixmap * FavoriteEditor::dialogIcon() const {
|
|---|
| 146 | return dialog_icon->pixmap();
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void FavoriteEditor::setData( FavoriteList list ) {
|
|---|
| 150 | table->setRowCount(list.count());
|
|---|
| 151 |
|
|---|
| 152 | for (int n = 0; n < list.count(); n++) {
|
|---|
| 153 | QTableWidgetItem * icon_item = new QTableWidgetItem;
|
|---|
| 154 | icon_item->setIcon( QIcon(list[n].icon()) );
|
|---|
| 155 | icon_item->setData( Qt::UserRole, list[n].icon() );
|
|---|
| 156 | icon_item->setData( Qt::ToolTipRole, list[n].icon() );
|
|---|
| 157 | icon_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
|---|
| 158 |
|
|---|
| 159 | QTableWidgetItem * name_item = new QTableWidgetItem;
|
|---|
| 160 | name_item->setText( list[n].name() );
|
|---|
| 161 |
|
|---|
| 162 | QTableWidgetItem * file_item = new QTableWidgetItem;
|
|---|
| 163 | file_item->setData( Qt::ToolTipRole, list[n].file() );
|
|---|
| 164 | file_item->setData( Qt::UserRole, list[n].isSubentry() );
|
|---|
| 165 | if (list[n].isSubentry()) {
|
|---|
| 166 | file_item->setFlags(Qt::ItemIsSelectable);
|
|---|
| 167 | file_item->setData( Qt::UserRole + 1, list[n].file() );
|
|---|
| 168 | file_item->setText( tr("Favorite list") );
|
|---|
| 169 | } else {
|
|---|
| 170 | file_item->setText( list[n].file() );
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | table->setItem(n, COL_ICON, icon_item);
|
|---|
| 174 | table->setItem(n, COL_NAME, name_item);
|
|---|
| 175 | table->setItem(n, COL_FILE, file_item);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | //table->resizeColumnsToContents();
|
|---|
| 179 |
|
|---|
| 180 | //table->setCurrentCell(0, 0);
|
|---|
| 181 | table->setCurrentCell(table->rowCount()-1, 0);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | FavoriteList FavoriteEditor::data() {
|
|---|
| 185 | FavoriteList list;
|
|---|
| 186 |
|
|---|
| 187 | for (int n = 0; n < table->rowCount(); n++) {
|
|---|
| 188 | Favorite f;
|
|---|
| 189 | f.setName( table->item(n, COL_NAME)->text() );
|
|---|
| 190 | f.setIcon( table->item(n, COL_ICON)->data(Qt::UserRole).toString() );
|
|---|
| 191 | f.setSubentry( table->item(n, COL_FILE)->data(Qt::UserRole).toBool() );
|
|---|
| 192 | if (f.isSubentry()) {
|
|---|
|
|---|