source: smplayer/trunk/src/bookmarkdialog.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

File size: 4.5 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 "bookmarkdialog.h"
20#include "helper.h"
21
22#include <QHeaderView>
23#include <QTimeEdit>
24#include <QItemDelegate>
25
26#define COL_TIME 0
27#define COL_NAME 1
28
29class BEDelegate : public QItemDelegate
30{
31public:
32 BEDelegate(QObject *parent = 0);
33
34 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
35 const QModelIndex &index) const;
36 virtual void setModelData(QWidget * editor, QAbstractItemModel * model,
37 const QModelIndex & index ) const;
38};
39
40BEDelegate::BEDelegate(QObject *parent) : QItemDelegate(parent) {
41}
42
43QWidget * BEDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
44 //qDebug("BEDelegate::createEditor");
45
46 if (index.column() == COL_TIME) {
47 QTimeEdit * e = new QTimeEdit(parent);
48 e->setDisplayFormat("hh:mm:ss");
49 int time = index.model()->data(index, Qt::UserRole).toInt();
50 QTime t(0,0);
51 e->setTime(t.addSecs(time));
52 return e;
53 }
54 else {
55 return QItemDelegate::createEditor(parent, option, index);
56 }
57}
58
59void BEDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
60 if (index.column() == COL_TIME) {
61 QTimeEdit * e = static_cast<QTimeEdit*>(editor);
62 QTime t(0,0);
63 int time = t.secsTo(e->time());
64 model->setData(index, Helper::formatTime(time));
65 model->setData(index, time, Qt::UserRole);
66 }
67}
68
69
70BookmarkDialog::BookmarkDialog( QWidget* parent, Qt::WindowFlags f )
71 : QDialog(parent, f)
72{
73 setupUi(this);
74
75 table->setColumnCount(2);
76 table->setHorizontalHeaderLabels(QStringList() << tr("Time") << tr("Name") );
77
78 table->setAlternatingRowColors(true);
79#if QT_VERSION >= 0x050000
80 table->horizontalHeader()->setSectionResizeMode(COL_NAME, QHeaderView::Stretch);
81#else
82 table->horizontalHeader()->setResizeMode(COL_NAME, QHeaderView::Stretch);
83#endif
84
85 table->setSelectionBehavior(QAbstractItemView::SelectRows);
86 table->setSelectionMode(QAbstractItemView::ExtendedSelection);
87
88 table->setItemDelegateForColumn( COL_TIME, new BEDelegate(table) );
89}
90
91BookmarkDialog::~BookmarkDialog() {
92}
93
94void BookmarkDialog::setBookmarks(QMap<int, QString> b) {
95 table->setRowCount(b.count());
96 int n = 0;
97 QMap<int, QString>::const_iterator i = b.constBegin();
98 while (i != b.constEnd()) {
99 QString name = i.value();
100 int time = i.key();
101
102 QTableWidgetItem * time_item = new QTableWidgetItem;
103 time_item->setText(Helper::formatTime(time));
104 time_item->setData(Qt::UserRole, time);
105
106 QTableWidgetItem * name_item = new QTableWidgetItem;
107 name_item->setText(name);
108
109 table->setItem(n, COL_TIME, time_item);
110 table->setItem(n, COL_NAME, name_item);
111
112 i++;
113 n++;
114 }
115}
116
117QMap<int, QString> BookmarkDialog::bookmarks() {
118 QMap<int, QString> b;
119
120 for (int n = 0; n < table->rowCount(); n++) {
121 int time = table->item(n, COL_TIME)->data(Qt::UserRole).toInt();
122 QString name = table->item(n, COL_NAME)->text();
123 b.insert(time, name);
124 }
125
126 return b;
127}
128
129void BookmarkDialog::on_delete_button_clicked() {
130 int row = table->currentRow();
131 qDebug("BookmarkDialog::on_delete_button_clicked: current_row: %d", row);
132
133 if (row > -1) table->removeRow(row);
134
135 if (row >= table->rowCount()) row--;
136 table->setCurrentCell(row, table->currentColumn());
137}
138
139void BookmarkDialog::on_add_button_clicked() {
140 int row = table->currentRow();
141 qDebug("BookmarkDialog::on_add_button_clicked: current_row: %d", row);
142 row++;
143 table->insertRow(row);
144
145 QTableWidgetItem * time_item = new QTableWidgetItem;
146 int time = 0;
147 time_item->setText(Helper::formatTime(time));
148 time_item->setData(Qt::UserRole, time);
149
150 table->setItem(row, COL_TIME, time_item);
151 table->setItem(row, COL_NAME, new QTableWidgetItem);
152
153 table->setCurrentCell(row, table->currentColumn());
154}
155
156#include "moc_bookmarkdialog.cpp"
Note: See TracBrowser for help on using the repository browser.