annotate widgets/ModelDataTableDialog.cpp @ 675:3437e0fad7ae imaf_enc

IMAF load code from Jesus Corral Garcia
author Chris Cannam
date Mon, 04 Nov 2013 17:09:36 +0000
parents 4806715f7a19
children 97ea68f62c1f
rev   line source
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@392 33 #include <QDesktopWidget>
Chris@399 34 #include <QAction>
Chris@399 35 #include <QToolBar>
Chris@392 36
Chris@393 37 #include <iostream>
Chris@393 38
Chris@404 39 ModelDataTableDialog::ModelDataTableDialog(TabularModel *model,
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@402 54 QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
Chris@402 55 action->setStatusTip(tr("Toggle tracking of playback position"));
Chris@402 56 action->setCheckable(true);
Chris@404 57 action->setChecked(m_trackPlayback);
Chris@402 58 connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
Chris@402 59 toolbar->addAction(action);
Chris@402 60
Chris@404 61 toolbar = addToolBar(tr("Edit Toolbar"));
Chris@402 62
Chris@402 63 action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
Chris@399 64 action->setShortcut(tr("Insert"));
Chris@399 65 action->setStatusTip(tr("Insert a new item"));
Chris@399 66 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
Chris@399 67 toolbar->addAction(action);
Chris@399 68
Chris@399 69 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
Chris@399 70 action->setShortcut(tr("Delete"));
Chris@399 71 action->setStatusTip(tr("Delete the selected item or items"));
Chris@400 72 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
Chris@399 73 toolbar->addAction(action);
Chris@399 74
Chris@404 75 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@404 76
Chris@404 77 /*
Chris@399 78 action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
Chris@399 79 action->setShortcut(tr("Edit"));
Chris@399 80 action->setStatusTip(tr("Edit the selected item"));
Chris@399 81 connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
Chris@399 82 toolbar->addAction(action);
Chris@404 83 */
Chris@399 84
Chris@396 85 QFrame *mainFrame = new QFrame;
Chris@396 86 setCentralWidget(mainFrame);
Chris@396 87
Chris@392 88 QGridLayout *grid = new QGridLayout;
Chris@396 89 mainFrame->setLayout(grid);
Chris@392 90
Chris@392 91 QGroupBox *box = new QGroupBox;
Chris@398 92 if (title != "") {
Chris@398 93 box->setTitle(title);
Chris@398 94 } else {
Chris@398 95 box->setTitle(tr("Data in Layer"));
Chris@398 96 }
Chris@392 97 grid->addWidget(box, 0, 0);
Chris@392 98 grid->setRowStretch(0, 15);
Chris@392 99
Chris@392 100 QGridLayout *subgrid = new QGridLayout;
Chris@392 101 box->setLayout(subgrid);
Chris@392 102
Chris@392 103 subgrid->setSpacing(0);
Chris@392 104 subgrid->setMargin(5);
Chris@392 105
Chris@552 106 subgrid->addWidget(new QLabel(tr("Find:")), 1, 0);
Chris@552 107 subgrid->addWidget(new QLabel(tr(" ")), 1, 1);
Chris@552 108 m_find = new QLineEdit;
Chris@552 109 subgrid->addWidget(m_find, 1, 2);
Chris@552 110 connect(m_find, SIGNAL(textChanged(const QString &)),
Chris@552 111 this, SLOT(searchTextChanged(const QString &)));
Chris@552 112 connect(m_find, SIGNAL(returnPressed()),
Chris@552 113 this, SLOT(searchRepeated()));
Chris@552 114
Chris@392 115 m_tableView = new QTableView;
Chris@552 116 subgrid->addWidget(m_tableView, 0, 0, 1, 3);
Chris@392 117
Chris@395 118 m_tableView->setSortingEnabled(true);
Chris@396 119 m_tableView->sortByColumn(0, Qt::AscendingOrder);
Chris@392 120
Chris@392 121 m_table = new ModelDataTableModel(model);
Chris@392 122 m_tableView->setModel(m_table);
Chris@392 123
Chris@552 124 m_tableView->horizontalHeader()->setStretchLastSection(true);
Chris@552 125
Chris@393 126 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
Chris@393 127 this, SLOT(viewClicked(const QModelIndex &)));
Chris@393 128 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
Chris@393 129 this, SLOT(viewPressed(const QModelIndex &)));
Chris@400 130 connect(m_tableView->selectionModel(),
Chris@400 131 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
Chris@400 132 this,
Chris@400 133 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
Chris@400 134 connect(m_table, SIGNAL(addCommand(Command *)),
Chris@400 135 this, SLOT(addCommand(Command *)));
Chris@401 136 connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
Chris@401 137 this, SLOT(currentChangedThroughResort(const QModelIndex &)));
Chris@428 138 connect(m_table, SIGNAL(modelRemoved()),
Chris@428 139 this, SLOT(modelRemoved()));
Chris@393 140
Chris@392 141 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@396 142 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@392 143 grid->addWidget(bb, 2, 0);
Chris@392 144 grid->setRowStretch(2, 0);
Chris@392 145
Chris@392 146 QDesktopWidget *desktop = QApplication::desktop();
Chris@392 147 QRect available = desktop->availableGeometry();
Chris@392 148
Chris@392 149 int width = available.width() / 3;
Chris@392 150 int height = available.height() / 2;
Chris@392 151 if (height < 370) {
Chris@392 152 if (available.height() > 500) height = 370;
Chris@392 153 }
Chris@552 154 if (width < 650) {
Chris@552 155 if (available.width() > 750) width = 650;
Chris@552 156 else if (width < 500) {
Chris@552 157 if (available.width() > 650) width = 500;
Chris@552 158 }
Chris@392 159 }
Chris@392 160
Chris@392 161 resize(width, height);
Chris@392 162 }
Chris@392 163
Chris@392 164 ModelDataTableDialog::~ModelDataTableDialog()
Chris@392 165 {
Chris@392 166 delete m_table;
Chris@392 167 }
Chris@392 168
Chris@393 169 void
Chris@401 170 ModelDataTableDialog::userScrolledToFrame(unsigned long frame)
Chris@393 171 {
Chris@401 172 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 173 makeCurrent(index.row());
Chris@401 174 }
Chris@401 175
Chris@401 176 void
Chris@401 177 ModelDataTableDialog::playbackScrolledToFrame(unsigned long frame)
Chris@401 178 {
Chris@401 179 if (m_trackPlayback) {
Chris@401 180 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 181 makeCurrent(index.row());
Chris@401 182 }
Chris@401 183 }
Chris@401 184
Chris@401 185 void
Chris@552 186 ModelDataTableDialog::searchTextChanged(const QString &text)
Chris@552 187 {
Chris@552 188 QModelIndex mi = m_table->findText(text);
Chris@552 189 if (mi.isValid()) {
Chris@552 190 makeCurrent(mi.row());
Chris@552 191 m_tableView->selectionModel()->setCurrentIndex
Chris@552 192 (mi, QItemSelectionModel::ClearAndSelect);
Chris@552 193 }
Chris@552 194 }
Chris@552 195
Chris@552 196 void
Chris@552 197 ModelDataTableDialog::searchRepeated()
Chris@552 198 {
Chris@552 199 QModelIndex mi = m_table->findText(m_find->text());
Chris@552 200 if (mi.isValid()) {
Chris@552 201 makeCurrent(mi.row());
Chris@552 202 m_tableView->selectionModel()->setCurrentIndex
Chris@552 203 (mi, QItemSelectionModel::ClearAndSelect);
Chris@552 204 }
Chris@552 205 }
Chris@552 206
Chris@552 207 void
Chris@401 208 ModelDataTableDialog::makeCurrent(int row)
Chris@401 209 {
Chris@401 210 int rh = m_tableView->height() / m_tableView->rowHeight(0);
Chris@404 211 int topRow = row - rh/4;
Chris@401 212 if (topRow < 0) topRow = 0;
Chris@404 213
Chris@404 214 // should only scroll if the desired row is not currently visible
Chris@404 215
Chris@404 216 // should only select if no part of the desired row is currently selected
Chris@404 217
Chris@405 218 // std::cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
Chris@405 219 // << topRow << std::endl;
Chris@404 220
Chris@404 221 int pos = m_tableView->rowViewportPosition(row);
Chris@404 222
Chris@404 223 if (pos < 0 || pos >= m_tableView->height() - rh) {
Chris@404 224 m_tableView->scrollTo(m_table->index(topRow, 0));
Chris@404 225 }
Chris@404 226
Chris@404 227 bool haveRowSelected = false;
Chris@404 228 for (int i = 0; i < m_table->columnCount(); ++i) {
Chris@404 229 if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
Chris@404 230 haveRowSelected = true;
Chris@404 231 break;
Chris@404 232 }
Chris@404 233 }
Chris@404 234
Chris@404 235 if (!haveRowSelected) {
Chris@404 236 m_tableView->selectionModel()->setCurrentIndex
Chris@404 237 (m_table->index(row, 0),
Chris@404 238 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
Chris@404 239 }
Chris@393 240 }
Chris@393 241
Chris@393 242 void
Chris@393 243 ModelDataTableDialog::viewClicked(const QModelIndex &index)
Chris@393 244 {
Chris@587 245 // SVDEBUG << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << endl;
Chris@394 246 emit scrollToFrame(m_table->getFrameForModelIndex(index));
Chris@393 247 }
Chris@393 248
Chris@393 249 void
Chris@393 250 ModelDataTableDialog::viewPressed(const QModelIndex &index)
Chris@393 251 {
Chris@587 252 // SVDEBUG << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << endl;
Chris@393 253 }
Chris@393 254
Chris@393 255 void
Chris@400 256 ModelDataTableDialog::currentChanged(const QModelIndex &current,
Chris@400 257 const QModelIndex &previous)
Chris@399 258 {
Chris@587 259 // SVDEBUG << "ModelDataTableDialog::currentChanged: from "
Chris@409 260 // << previous.row() << ", " << previous.column()
Chris@409 261 // << " to " << current.row() << ", " << current.column()
Chris@585 262 // << endl;
Chris@400 263 m_currentRow = current.row();
Chris@401 264 m_table->setCurrentRow(m_currentRow);
Chris@399 265 }
Chris@399 266
Chris@399 267 void
Chris@400 268 ModelDataTableDialog::insertRow()
Chris@399 269 {
Chris@400 270 m_table->insertRow(m_currentRow);
Chris@400 271 }
Chris@400 272
Chris@400 273 void
Chris@400 274 ModelDataTableDialog::deleteRows()
Chris@400 275 {
Chris@400 276 // not efficient
Chris@400 277 while (m_tableView->selectionModel()->hasSelection()) {
Chris@400 278 m_table->removeRow
Chris@400 279 (m_tableView->selectionModel()->selection().indexes().begin()->row());
Chris@400 280 }
Chris@399 281 }
Chris@399 282
Chris@399 283 void
Chris@399 284 ModelDataTableDialog::editRow()
Chris@399 285 {
Chris@399 286 }
Chris@399 287
Chris@399 288 void
Chris@400 289 ModelDataTableDialog::addCommand(Command *command)
Chris@393 290 {
Chris@397 291 CommandHistory::getInstance()->addCommand(command, false, true);
Chris@393 292 }
Chris@393 293
Chris@401 294 void
Chris@402 295 ModelDataTableDialog::togglePlayTracking()
Chris@402 296 {
Chris@402 297 m_trackPlayback = !m_trackPlayback;
Chris@402 298 }
Chris@402 299
Chris@402 300 void
Chris@401 301 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
Chris@401 302 {
Chris@587 303 // SVDEBUG << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << endl;
Chris@401 304 // m_tableView->scrollTo(index);
Chris@401 305 makeCurrent(index.row());
Chris@401 306 }
Chris@401 307
Chris@428 308 void
Chris@428 309 ModelDataTableDialog::modelRemoved()
Chris@428 310 {
Chris@428 311 close();
Chris@428 312 }
Chris@401 313
Chris@401 314