Chris@392
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@392
|
2
|
Chris@392
|
3 /*
|
Chris@392
|
4 Sonic Visualiser
|
Chris@392
|
5 An audio file viewer and annotation editor.
|
Chris@392
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@392
|
7 This file copyright 2008 QMUL.
|
Chris@392
|
8
|
Chris@392
|
9 This program is free software; you can redistribute it and/or
|
Chris@392
|
10 modify it under the terms of the GNU General Public License as
|
Chris@392
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@392
|
12 License, or (at your option) any later version. See the file
|
Chris@392
|
13 COPYING included with this distribution for more information.
|
Chris@392
|
14 */
|
Chris@392
|
15
|
Chris@392
|
16 #include "ModelDataTableDialog.h"
|
Chris@392
|
17
|
Chris@392
|
18 #include "data/model/ModelDataTableModel.h"
|
Chris@395
|
19 #include "data/model/TabularModel.h"
|
Chris@398
|
20 #include "data/model/Model.h"
|
Chris@392
|
21
|
Chris@393
|
22 #include "CommandHistory.h"
|
Chris@399
|
23 #include "IconLoader.h"
|
Chris@393
|
24
|
Chris@392
|
25 #include <QTableView>
|
Chris@552
|
26 #include <QLineEdit>
|
Chris@392
|
27 #include <QGridLayout>
|
Chris@552
|
28 #include <QLabel>
|
Chris@392
|
29 #include <QGroupBox>
|
Chris@392
|
30 #include <QDialogButtonBox>
|
Chris@392
|
31 #include <QHeaderView>
|
Chris@392
|
32 #include <QApplication>
|
Chris@1478
|
33 #include <QScreen>
|
Chris@399
|
34 #include <QAction>
|
Chris@399
|
35 #include <QToolBar>
|
Chris@392
|
36
|
Chris@393
|
37 #include <iostream>
|
Chris@393
|
38
|
Chris@1479
|
39 ModelDataTableDialog::ModelDataTableDialog(ModelId tabularModelId,
|
Chris@404
|
40 QString title, QWidget *parent) :
|
Chris@400
|
41 QMainWindow(parent),
|
Chris@401
|
42 m_currentRow(0),
|
Chris@404
|
43 m_trackPlayback(true)
|
Chris@392
|
44 {
|
Chris@392
|
45 setWindowTitle(tr("Data Editor"));
|
Chris@392
|
46
|
Chris@404
|
47 QToolBar *toolbar;
|
Chris@399
|
48
|
Chris@404
|
49 toolbar = addToolBar(tr("Playback Toolbar"));
|
Chris@404
|
50 m_playToolbar = toolbar;
|
Chris@404
|
51 toolbar = addToolBar(tr("Play Mode Toolbar"));
|
Chris@404
|
52
|
Chris@399
|
53 IconLoader il;
|
Chris@399
|
54
|
Chris@402
|
55 QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
|
Chris@402
|
56 action->setStatusTip(tr("Toggle tracking of playback position"));
|
Chris@402
|
57 action->setCheckable(true);
|
Chris@404
|
58 action->setChecked(m_trackPlayback);
|
Chris@402
|
59 connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
|
Chris@402
|
60 toolbar->addAction(action);
|
Chris@402
|
61
|
Chris@404
|
62 toolbar = addToolBar(tr("Edit Toolbar"));
|
Chris@402
|
63
|
Chris@1175
|
64 action = new QAction(il.load("draw"), tr("Insert New Item"), this);
|
Chris@399
|
65 action->setShortcut(tr("Insert"));
|
Chris@399
|
66 action->setStatusTip(tr("Insert a new item"));
|
Chris@399
|
67 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
|
Chris@399
|
68 toolbar->addAction(action);
|
Chris@399
|
69
|
Chris@399
|
70 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
|
Chris@399
|
71 action->setShortcut(tr("Delete"));
|
Chris@399
|
72 action->setStatusTip(tr("Delete the selected item or items"));
|
Chris@400
|
73 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
|
Chris@399
|
74 toolbar->addAction(action);
|
Chris@399
|
75
|
Chris@404
|
76 CommandHistory::getInstance()->registerToolbar(toolbar);
|
Chris@404
|
77
|
Chris@396
|
78 QFrame *mainFrame = new QFrame;
|
Chris@396
|
79 setCentralWidget(mainFrame);
|
Chris@396
|
80
|
Chris@392
|
81 QGridLayout *grid = new QGridLayout;
|
Chris@396
|
82 mainFrame->setLayout(grid);
|
Chris@392
|
83
|
Chris@392
|
84 QGroupBox *box = new QGroupBox;
|
Chris@398
|
85 if (title != "") {
|
Chris@398
|
86 box->setTitle(title);
|
Chris@398
|
87 } else {
|
Chris@398
|
88 box->setTitle(tr("Data in Layer"));
|
Chris@398
|
89 }
|
Chris@392
|
90 grid->addWidget(box, 0, 0);
|
Chris@392
|
91 grid->setRowStretch(0, 15);
|
Chris@392
|
92
|
Chris@392
|
93 QGridLayout *subgrid = new QGridLayout;
|
Chris@392
|
94 box->setLayout(subgrid);
|
Chris@392
|
95
|
Chris@392
|
96 subgrid->setSpacing(0);
|
Chris@392
|
97 subgrid->setMargin(5);
|
Chris@392
|
98
|
Chris@552
|
99 subgrid->addWidget(new QLabel(tr("Find:")), 1, 0);
|
Chris@552
|
100 subgrid->addWidget(new QLabel(tr(" ")), 1, 1);
|
Chris@552
|
101 m_find = new QLineEdit;
|
Chris@552
|
102 subgrid->addWidget(m_find, 1, 2);
|
Chris@552
|
103 connect(m_find, SIGNAL(textChanged(const QString &)),
|
Chris@552
|
104 this, SLOT(searchTextChanged(const QString &)));
|
Chris@552
|
105 connect(m_find, SIGNAL(returnPressed()),
|
Chris@552
|
106 this, SLOT(searchRepeated()));
|
Chris@552
|
107
|
Chris@392
|
108 m_tableView = new QTableView;
|
Chris@552
|
109 subgrid->addWidget(m_tableView, 0, 0, 1, 3);
|
Chris@392
|
110
|
Chris@395
|
111 m_tableView->setSortingEnabled(true);
|
Chris@396
|
112 m_tableView->sortByColumn(0, Qt::AscendingOrder);
|
Chris@392
|
113
|
Chris@1479
|
114 m_table = new ModelDataTableModel(tabularModelId);
|
Chris@392
|
115 m_tableView->setModel(m_table);
|
Chris@392
|
116
|
Chris@552
|
117 m_tableView->horizontalHeader()->setStretchLastSection(true);
|
Chris@552
|
118
|
Chris@393
|
119 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
|
Chris@393
|
120 this, SLOT(viewClicked(const QModelIndex &)));
|
Chris@393
|
121 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
|
Chris@393
|
122 this, SLOT(viewPressed(const QModelIndex &)));
|
Chris@400
|
123 connect(m_tableView->selectionModel(),
|
Chris@400
|
124 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
|
Chris@400
|
125 this,
|
Chris@400
|
126 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
|
Chris@400
|
127 connect(m_table, SIGNAL(addCommand(Command *)),
|
Chris@400
|
128 this, SLOT(addCommand(Command *)));
|
Chris@401
|
129 connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
|
Chris@401
|
130 this, SLOT(currentChangedThroughResort(const QModelIndex &)));
|
Chris@428
|
131 connect(m_table, SIGNAL(modelRemoved()),
|
Chris@428
|
132 this, SLOT(modelRemoved()));
|
Chris@393
|
133
|
Chris@392
|
134 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
|
Chris@396
|
135 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
|
Chris@392
|
136 grid->addWidget(bb, 2, 0);
|
Chris@392
|
137 grid->setRowStretch(2, 0);
|
Chris@392
|
138
|
Chris@1478
|
139 QScreen *screen = QGuiApplication::primaryScreen();
|
Chris@1478
|
140 QRect available = screen->availableGeometry();
|
Chris@392
|
141
|
Chris@392
|
142 int width = available.width() / 3;
|
Chris@392
|
143 int height = available.height() / 2;
|
Chris@392
|
144 if (height < 370) {
|
Chris@392
|
145 if (available.height() > 500) height = 370;
|
Chris@392
|
146 }
|
Chris@552
|
147 if (width < 650) {
|
Chris@552
|
148 if (available.width() > 750) width = 650;
|
Chris@552
|
149 else if (width < 500) {
|
Chris@552
|
150 if (available.width() > 650) width = 500;
|
Chris@552
|
151 }
|
Chris@392
|
152 }
|
Chris@392
|
153
|
Chris@392
|
154 resize(width, height);
|
Chris@392
|
155 }
|
Chris@392
|
156
|
Chris@392
|
157 ModelDataTableDialog::~ModelDataTableDialog()
|
Chris@392
|
158 {
|
Chris@392
|
159 delete m_table;
|
Chris@392
|
160 }
|
Chris@392
|
161
|
Chris@393
|
162 void
|
Chris@908
|
163 ModelDataTableDialog::userScrolledToFrame(sv_frame_t frame)
|
Chris@393
|
164 {
|
Chris@401
|
165 QModelIndex index = m_table->getModelIndexForFrame(frame);
|
Chris@401
|
166 makeCurrent(index.row());
|
Chris@401
|
167 }
|
Chris@401
|
168
|
Chris@401
|
169 void
|
Chris@908
|
170 ModelDataTableDialog::playbackScrolledToFrame(sv_frame_t frame)
|
Chris@401
|
171 {
|
Chris@401
|
172 if (m_trackPlayback) {
|
Chris@401
|
173 QModelIndex index = m_table->getModelIndexForFrame(frame);
|
Chris@401
|
174 makeCurrent(index.row());
|
Chris@401
|
175 }
|
Chris@401
|
176 }
|
Chris@401
|
177
|
Chris@401
|
178 void
|
Chris@552
|
179 ModelDataTableDialog::searchTextChanged(const QString &text)
|
Chris@552
|
180 {
|
Chris@552
|
181 QModelIndex mi = m_table->findText(text);
|
Chris@552
|
182 if (mi.isValid()) {
|
Chris@552
|
183 makeCurrent(mi.row());
|
Chris@552
|
184 m_tableView->selectionModel()->setCurrentIndex
|
Chris@552
|
185 (mi, QItemSelectionModel::ClearAndSelect);
|
Chris@552
|
186 }
|
Chris@552
|
187 }
|
Chris@552
|
188
|
Chris@552
|
189 void
|
Chris@552
|
190 ModelDataTableDialog::searchRepeated()
|
Chris@552
|
191 {
|
Chris@552
|
192 QModelIndex mi = m_table->findText(m_find->text());
|
Chris@552
|
193 if (mi.isValid()) {
|
Chris@552
|
194 makeCurrent(mi.row());
|
Chris@552
|
195 m_tableView->selectionModel()->setCurrentIndex
|
Chris@552
|
196 (mi, QItemSelectionModel::ClearAndSelect);
|
Chris@552
|
197 }
|
Chris@552
|
198 }
|
Chris@552
|
199
|
Chris@552
|
200 void
|
Chris@401
|
201 ModelDataTableDialog::makeCurrent(int row)
|
Chris@401
|
202 {
|
Chris@1271
|
203 if (m_table->rowCount() == 0 ||
|
Chris@1271
|
204 row >= m_table->rowCount() ||
|
Chris@1271
|
205 row < 0) {
|
Chris@1271
|
206 return;
|
Chris@1271
|
207 }
|
Chris@1271
|
208
|
Chris@401
|
209 int rh = m_tableView->height() / m_tableView->rowHeight(0);
|
Chris@404
|
210 int topRow = row - rh/4;
|
Chris@401
|
211 if (topRow < 0) topRow = 0;
|
Chris@404
|
212
|
Chris@404
|
213 // should only scroll if the desired row is not currently visible
|
Chris@404
|
214
|
Chris@404
|
215 // should only select if no part of the desired row is currently selected
|
Chris@404
|
216
|
Chris@682
|
217 // cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
|
Chris@682
|
218 // << topRow << endl;
|
Chris@404
|
219
|
Chris@404
|
220 int pos = m_tableView->rowViewportPosition(row);
|
Chris@404
|
221
|
Chris@404
|
222 if (pos < 0 || pos >= m_tableView->height() - rh) {
|
Chris@404
|
223 m_tableView->scrollTo(m_table->index(topRow, 0));
|
Chris@404
|
224 }
|
Chris@404
|
225
|
Chris@404
|
226 bool haveRowSelected = false;
|
Chris@404
|
227 for (int i = 0; i < m_table->columnCount(); ++i) {
|
Chris@404
|
228 if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
|
Chris@404
|
229 haveRowSelected = true;
|
Chris@404
|
230 break;
|
Chris@404
|
231 }
|
Chris@404
|
232 }
|
Chris@404
|
233
|
Chris@404
|
234 if (!haveRowSelected) {
|
Chris@404
|
235 m_tableView->selectionModel()->setCurrentIndex
|
Chris@404
|
236 (m_table->index(row, 0),
|
Chris@404
|
237 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
Chris@404
|
238 }
|
Chris@393
|
239 }
|
Chris@393
|
240
|
Chris@393
|
241 void
|
Chris@393
|
242 ModelDataTableDialog::viewClicked(const QModelIndex &index)
|
Chris@393
|
243 {
|
Chris@587
|
244 // SVDEBUG << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << endl;
|
Chris@394
|
245 emit scrollToFrame(m_table->getFrameForModelIndex(index));
|
Chris@393
|
246 }
|
Chris@393
|
247
|
Chris@393
|
248 void
|
Chris@807
|
249 ModelDataTableDialog::viewPressed(const QModelIndex &)
|
Chris@393
|
250 {
|
Chris@587
|
251 // SVDEBUG << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << endl;
|
Chris@393
|
252 }
|
Chris@393
|
253
|
Chris@393
|
254 void
|
Chris@400
|
255 ModelDataTableDialog::currentChanged(const QModelIndex ¤t,
|
Chris@1272
|
256 const QModelIndex &previous)
|
Chris@399
|
257 {
|
Chris@1272
|
258 SVDEBUG << "ModelDataTableDialog::currentChanged: from "
|
Chris@1272
|
259 << previous.row() << ", " << previous.column()
|
Chris@1272
|
260 << " to " << current.row() << ", " << current.column()
|
Chris@1272
|
261 << endl;
|
Chris@400
|
262 m_currentRow = current.row();
|
Chris@401
|
263 m_table->setCurrentRow(m_currentRow);
|
Chris@399
|
264 }
|
Chris@399
|
265
|
Chris@399
|
266 void
|
Chris@400
|
267 ModelDataTableDialog::insertRow()
|
Chris@399
|
268 {
|
Chris@400
|
269 m_table->insertRow(m_currentRow);
|
Chris@400
|
270 }
|
Chris@400
|
271
|
Chris@400
|
272 void
|
Chris@400
|
273 ModelDataTableDialog::deleteRows()
|
Chris@400
|
274 {
|
Chris@1272
|
275 std::set<int> selectedRows;
|
Chris@1272
|
276 if (m_tableView->selectionModel()->hasSelection()) {
|
Chris@1272
|
277 for (const auto &ix: m_tableView->selectionModel()->selectedIndexes()) {
|
Chris@1272
|
278 selectedRows.insert(ix.row());
|
Chris@1272
|
279 }
|
Chris@1272
|
280 }
|
Chris@1272
|
281 // Remove rows in reverse order, so as not to pull the rug from
|
Chris@1272
|
282 // under our own feet
|
Chris@1272
|
283 for (auto ri = selectedRows.rbegin(); ri != selectedRows.rend(); ++ri) {
|
Chris@1272
|
284 SVDEBUG << "ModelDataTableDialog: removing row " << *ri << endl;
|
Chris@1272
|
285 m_table->removeRow(*ri);
|
Chris@400
|
286 }
|
Chris@399
|
287 }
|
Chris@399
|
288
|
Chris@399
|
289 void
|
Chris@399
|
290 ModelDataTableDialog::editRow()
|
Chris@399
|
291 {
|
Chris@399
|
292 }
|
Chris@399
|
293
|
Chris@399
|
294 void
|
Chris@400
|
295 ModelDataTableDialog::addCommand(Command *command)
|
Chris@393
|
296 {
|
Chris@397
|
297 CommandHistory::getInstance()->addCommand(command, false, true);
|
Chris@393
|
298 }
|
Chris@393
|
299
|
Chris@401
|
300 void
|
Chris@402
|
301 ModelDataTableDialog::togglePlayTracking()
|
Chris@402
|
302 {
|
Chris@402
|
303 m_trackPlayback = !m_trackPlayback;
|
Chris@402
|
304 }
|
Chris@402
|
305
|
Chris@402
|
306 void
|
Chris@401
|
307 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
|
Chris@401
|
308 {
|
Chris@587
|
309 // SVDEBUG << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << endl;
|
Chris@401
|
310 // m_tableView->scrollTo(index);
|
Chris@401
|
311 makeCurrent(index.row());
|
Chris@401
|
312 }
|
Chris@401
|
313
|
Chris@428
|
314 void
|
Chris@428
|
315 ModelDataTableDialog::modelRemoved()
|
Chris@428
|
316 {
|
Chris@428
|
317 close();
|
Chris@428
|
318 }
|
Chris@401
|
319
|
Chris@401
|
320
|