summaryrefslogtreecommitdiff
path: root/src/bookmarkdialog.cpp
blob: da68cc5564b637849de51cf51a1c641e7ca4b1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "bookmarkdialog.h"
#include "helper.h"

#include <QHeaderView>
#include <QTimeEdit>
#include <QItemDelegate>

#define COL_TIME 0
#define COL_NAME 1

class BEDelegate : public QItemDelegate
{
public:
	BEDelegate(QObject *parent = 0);

	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const;
	virtual void setModelData(QWidget * editor, QAbstractItemModel * model, 
                              const QModelIndex & index ) const;
};

BEDelegate::BEDelegate(QObject *parent) : QItemDelegate(parent) {
}

QWidget * BEDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index) const {
	//qDebug("BEDelegate::createEditor");

	if (index.column() == COL_TIME) {
		QTimeEdit * e = new QTimeEdit(parent);
		e->setDisplayFormat("hh:mm:ss");
		int time = index.model()->data(index, Qt::UserRole).toInt();
		QTime t(0,0);
		e->setTime(t.addSecs(time));
		return e;
	}
	else {
		return QItemDelegate::createEditor(parent, option, index);
	}
}

void BEDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
	if (index.column() == COL_TIME) {
		QTimeEdit * e = static_cast<QTimeEdit*>(editor);
		QTime t(0,0);
		int time = t.secsTo(e->time());
		model->setData(index, Helper::formatTime(time));
		model->setData(index, time, Qt::UserRole);
	}
}


BookmarkDialog::BookmarkDialog( QWidget* parent, Qt::WindowFlags f )
	: QDialog(parent, f)
{
	setupUi(this);

	table->setColumnCount(2);
	table->setHorizontalHeaderLabels(QStringList() << tr("Time") << tr("Name") );

	table->setAlternatingRowColors(true);
#if QT_VERSION >= 0x050000
	table->horizontalHeader()->setSectionResizeMode(COL_NAME, QHeaderView::Stretch);
#else
	table->horizontalHeader()->setResizeMode(COL_NAME, QHeaderView::Stretch);
#endif

	table->setSelectionBehavior(QAbstractItemView::SelectRows);
	table->setSelectionMode(QAbstractItemView::ExtendedSelection);

	table->setItemDelegateForColumn( COL_TIME, new BEDelegate(table) );
}

BookmarkDialog::~BookmarkDialog() {
}

void BookmarkDialog::setBookmarks(QMap<int, QString> b) {
	table->setRowCount(b.count());
	int n = 0;
	QMap<int, QString>::const_iterator i = b.constBegin();
	while (i != b.constEnd()) {
		QString name = i.value();
		int time = i.key();

		QTableWidgetItem * time_item = new QTableWidgetItem;
		time_item->setText(Helper::formatTime(time));
		time_item->setData(Qt::UserRole, time);

		QTableWidgetItem * name_item = new QTableWidgetItem;
		name_item->setText(name);

		table->setItem(n, COL_TIME, time_item);
		table->setItem(n, COL_NAME, name_item);

		i++;
		n++;
	}
}

QMap<int, QString> BookmarkDialog::bookmarks() {
	QMap<int, QString> b;

	for (int n = 0; n < table->rowCount(); n++) {
		int time = table->item(n, COL_TIME)->data(Qt::UserRole).toInt();
		QString name = table->item(n, COL_NAME)->text();
		b.insert(time, name);
	}

	return b;
}

void BookmarkDialog::on_delete_button_clicked() {
	int row = table->currentRow();
	qDebug("BookmarkDialog::on_delete_button_clicked: current_row: %d", row);

	if (row > -1) table->removeRow(row);

	if (row >= table->rowCount()) row--;
	table->setCurrentCell(row, table->currentColumn());
}

void BookmarkDialog::on_add_button_clicked() {
	int row = table->currentRow();
	qDebug("BookmarkDialog::on_add_button_clicked: current_row: %d", row);
	row++;
	table->insertRow(row);

	QTableWidgetItem * time_item = new QTableWidgetItem;
	int time = 0;
	time_item->setText(Helper::formatTime(time));
	time_item->setData(Qt::UserRole, time);

	table->setItem(row, COL_TIME, time_item);
	table->setItem(row, COL_NAME, new QTableWidgetItem);

	table->setCurrentCell(row, table->currentColumn());
}

#include "moc_bookmarkdialog.cpp"