annotate widgets/ModelDataTableDialog.cpp @ 401:96531861b2f3

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