annotate widgets/ModelDataTableDialog.cpp @ 404:4075bf03faef

* Add playback controls to data edit dialog * Make data edit dialog close properly when layer is removed from view * More fixes to playback tracking & selection model in data edit dialog * Remove edit button from data edit dialog for now
author Chris Cannam
date Wed, 18 Jun 2008 15:53:23 +0000
parents dc32f6e7839b
children 7920688d8e70
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@392 26 #include <QGridLayout>
Chris@392 27 #include <QGroupBox>
Chris@392 28 #include <QDialogButtonBox>
Chris@392 29 #include <QHeaderView>
Chris@392 30 #include <QApplication>
Chris@392 31 #include <QDesktopWidget>
Chris@399 32 #include <QAction>
Chris@399 33 #include <QToolBar>
Chris@392 34
Chris@393 35 #include <iostream>
Chris@393 36
Chris@404 37 ModelDataTableDialog::ModelDataTableDialog(TabularModel *model,
Chris@404 38 QString title, QWidget *parent) :
Chris@400 39 QMainWindow(parent),
Chris@401 40 m_currentRow(0),
Chris@404 41 m_trackPlayback(true)
Chris@392 42 {
Chris@392 43 setWindowTitle(tr("Data Editor"));
Chris@392 44
Chris@404 45 QToolBar *toolbar;
Chris@399 46
Chris@404 47 toolbar = addToolBar(tr("Playback Toolbar"));
Chris@404 48 m_playToolbar = toolbar;
Chris@404 49 toolbar = addToolBar(tr("Play Mode Toolbar"));
Chris@404 50
Chris@399 51 IconLoader il;
Chris@399 52
Chris@402 53 QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
Chris@402 54 action->setStatusTip(tr("Toggle tracking of playback position"));
Chris@402 55 action->setCheckable(true);
Chris@404 56 action->setChecked(m_trackPlayback);
Chris@402 57 connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
Chris@402 58 toolbar->addAction(action);
Chris@402 59
Chris@404 60 toolbar = addToolBar(tr("Edit Toolbar"));
Chris@402 61
Chris@402 62 action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
Chris@399 63 action->setShortcut(tr("Insert"));
Chris@399 64 action->setStatusTip(tr("Insert a new item"));
Chris@399 65 connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
Chris@399 66 toolbar->addAction(action);
Chris@399 67
Chris@399 68 action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
Chris@399 69 action->setShortcut(tr("Delete"));
Chris@399 70 action->setStatusTip(tr("Delete the selected item or items"));
Chris@400 71 connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
Chris@399 72 toolbar->addAction(action);
Chris@399 73
Chris@404 74 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@404 75
Chris@404 76 /*
Chris@399 77 action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
Chris@399 78 action->setShortcut(tr("Edit"));
Chris@399 79 action->setStatusTip(tr("Edit the selected item"));
Chris@399 80 connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
Chris@399 81 toolbar->addAction(action);
Chris@404 82 */
Chris@399 83
Chris@396 84 QFrame *mainFrame = new QFrame;
Chris@396 85 setCentralWidget(mainFrame);
Chris@396 86
Chris@392 87 QGridLayout *grid = new QGridLayout;
Chris@396 88 mainFrame->setLayout(grid);
Chris@392 89
Chris@392 90 QGroupBox *box = new QGroupBox;
Chris@398 91 if (title != "") {
Chris@398 92 box->setTitle(title);
Chris@398 93 } else {
Chris@398 94 box->setTitle(tr("Data in Layer"));
Chris@398 95 }
Chris@392 96 grid->addWidget(box, 0, 0);
Chris@392 97 grid->setRowStretch(0, 15);
Chris@392 98
Chris@392 99 QGridLayout *subgrid = new QGridLayout;
Chris@392 100 box->setLayout(subgrid);
Chris@392 101
Chris@392 102 subgrid->setSpacing(0);
Chris@392 103 subgrid->setMargin(5);
Chris@392 104
Chris@392 105 m_tableView = new QTableView;
Chris@392 106 subgrid->addWidget(m_tableView);
Chris@392 107
Chris@399 108 // m_tableView->verticalHeader()->hide();
Chris@392 109 // m_tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
Chris@395 110 m_tableView->setSortingEnabled(true);
Chris@396 111 m_tableView->sortByColumn(0, Qt::AscendingOrder);
Chris@392 112
Chris@392 113 m_table = new ModelDataTableModel(model);
Chris@392 114 m_tableView->setModel(m_table);
Chris@392 115
Chris@393 116 connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
Chris@393 117 this, SLOT(viewClicked(const QModelIndex &)));
Chris@393 118 connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
Chris@393 119 this, SLOT(viewPressed(const QModelIndex &)));
Chris@400 120 connect(m_tableView->selectionModel(),
Chris@400 121 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
Chris@400 122 this,
Chris@400 123 SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
Chris@400 124 connect(m_table, SIGNAL(addCommand(Command *)),
Chris@400 125 this, SLOT(addCommand(Command *)));
Chris@401 126 connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
Chris@401 127 this, SLOT(currentChangedThroughResort(const QModelIndex &)));
Chris@393 128
Chris@392 129 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@396 130 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@392 131 grid->addWidget(bb, 2, 0);
Chris@392 132 grid->setRowStretch(2, 0);
Chris@392 133
Chris@392 134 QDesktopWidget *desktop = QApplication::desktop();
Chris@392 135 QRect available = desktop->availableGeometry();
Chris@392 136
Chris@392 137 int width = available.width() / 3;
Chris@392 138 int height = available.height() / 2;
Chris@392 139 if (height < 370) {
Chris@392 140 if (available.height() > 500) height = 370;
Chris@392 141 }
Chris@392 142 if (width < 500) {
Chris@392 143 if (available.width() > 650) width = 500;
Chris@392 144 }
Chris@392 145
Chris@392 146 resize(width, height);
Chris@392 147 }
Chris@392 148
Chris@392 149 ModelDataTableDialog::~ModelDataTableDialog()
Chris@392 150 {
Chris@392 151 delete m_table;
Chris@392 152 }
Chris@392 153
Chris@393 154 void
Chris@401 155 ModelDataTableDialog::userScrolledToFrame(unsigned long frame)
Chris@393 156 {
Chris@401 157 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 158 makeCurrent(index.row());
Chris@401 159 }
Chris@401 160
Chris@401 161 void
Chris@401 162 ModelDataTableDialog::playbackScrolledToFrame(unsigned long frame)
Chris@401 163 {
Chris@401 164 if (m_trackPlayback) {
Chris@401 165 QModelIndex index = m_table->getModelIndexForFrame(frame);
Chris@401 166 makeCurrent(index.row());
Chris@401 167 }
Chris@401 168 }
Chris@401 169
Chris@401 170 void
Chris@401 171 ModelDataTableDialog::makeCurrent(int row)
Chris@401 172 {
Chris@401 173 int rh = m_tableView->height() / m_tableView->rowHeight(0);
Chris@404 174 int topRow = row - rh/4;
Chris@401 175 if (topRow < 0) topRow = 0;
Chris@404 176
Chris@404 177 // should only scroll if the desired row is not currently visible
Chris@404 178
Chris@404 179 // should only select if no part of the desired row is currently selected
Chris@404 180
Chris@403 181 std::cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
Chris@403 182 << topRow << std::endl;
Chris@404 183
Chris@404 184 int pos = m_tableView->rowViewportPosition(row);
Chris@404 185
Chris@404 186 if (pos < 0 || pos >= m_tableView->height() - rh) {
Chris@404 187 m_tableView->scrollTo(m_table->index(topRow, 0));
Chris@404 188 }
Chris@404 189
Chris@404 190 bool haveRowSelected = false;
Chris@404 191 for (int i = 0; i < m_table->columnCount(); ++i) {
Chris@404 192 if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
Chris@404 193 haveRowSelected = true;
Chris@404 194 break;
Chris@404 195 }
Chris@404 196 }
Chris@404 197
Chris@404 198 if (!haveRowSelected) {
Chris@404 199 m_tableView->selectionModel()->setCurrentIndex
Chris@404 200 (m_table->index(row, 0),
Chris@404 201 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
Chris@404 202 }
Chris@393 203 }
Chris@393 204
Chris@393 205 void
Chris@393 206 ModelDataTableDialog::viewClicked(const QModelIndex &index)
Chris@393 207 {
Chris@393 208 std::cerr << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << std::endl;
Chris@394 209 emit scrollToFrame(m_table->getFrameForModelIndex(index));
Chris@393 210 }
Chris@393 211
Chris@393 212 void
Chris@393 213 ModelDataTableDialog::viewPressed(const QModelIndex &index)
Chris@393 214 {
Chris@393 215 std::cerr << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << std::endl;
Chris@393 216 }
Chris@393 217
Chris@393 218 void
Chris@400 219 ModelDataTableDialog::currentChanged(const QModelIndex &current,
Chris@400 220 const QModelIndex &previous)
Chris@399 221 {
Chris@400 222 std::cerr << "ModelDataTableDialog::currentChanged: from "
Chris@400 223 << previous.row() << ", " << previous.column()
Chris@400 224 << " to " << current.row() << ", " << current.column()
Chris@400 225 << std::endl;
Chris@400 226 m_currentRow = current.row();
Chris@401 227 m_table->setCurrentRow(m_currentRow);
Chris@399 228 }
Chris@399 229
Chris@399 230 void
Chris@400 231 ModelDataTableDialog::insertRow()
Chris@399 232 {
Chris@400 233 m_table->insertRow(m_currentRow);
Chris@400 234 }
Chris@400 235
Chris@400 236 void
Chris@400 237 ModelDataTableDialog::deleteRows()
Chris@400 238 {
Chris@400 239 // not efficient
Chris@400 240 while (m_tableView->selectionModel()->hasSelection()) {
Chris@400 241 m_table->removeRow
Chris@400 242 (m_tableView->selectionModel()->selection().indexes().begin()->row());
Chris@400 243 }
Chris@399 244 }
Chris@399 245
Chris@399 246 void
Chris@399 247 ModelDataTableDialog::editRow()
Chris@399 248 {
Chris@399 249 }
Chris@399 250
Chris@399 251 void
Chris@400 252 ModelDataTableDialog::addCommand(Command *command)
Chris@393 253 {
Chris@397 254 CommandHistory::getInstance()->addCommand(command, false, true);
Chris@393 255 }
Chris@393 256
Chris@401 257 void
Chris@402 258 ModelDataTableDialog::togglePlayTracking()
Chris@402 259 {
Chris@402 260 m_trackPlayback = !m_trackPlayback;
Chris@402 261 }
Chris@402 262
Chris@402 263 void
Chris@401 264 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
Chris@401 265 {
Chris@401 266 std::cerr << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << std::endl;
Chris@401 267 // m_tableView->scrollTo(index);
Chris@401 268 makeCurrent(index.row());
Chris@401 269 }
Chris@401 270
Chris@401 271
Chris@401 272