annotate widgets/ModelDataTableDialog.cpp @ 402:66e01a6c9554

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