svgui  1.9
ModelDataTableDialog.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2008 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ModelDataTableDialog.h"
00017 
00018 #include "data/model/ModelDataTableModel.h"
00019 #include "data/model/TabularModel.h"
00020 #include "data/model/Model.h"
00021 
00022 #include "CommandHistory.h"
00023 #include "IconLoader.h"
00024 
00025 #include <QTableView>
00026 #include <QLineEdit>
00027 #include <QGridLayout>
00028 #include <QLabel>
00029 #include <QGroupBox>
00030 #include <QDialogButtonBox>
00031 #include <QHeaderView>
00032 #include <QApplication>
00033 #include <QDesktopWidget>
00034 #include <QAction>
00035 #include <QToolBar>
00036 
00037 #include <iostream>
00038 
00039 ModelDataTableDialog::ModelDataTableDialog(TabularModel *model,
00040                                            QString title, QWidget *parent) :
00041     QMainWindow(parent),
00042     m_currentRow(0),
00043     m_trackPlayback(true)
00044 {
00045     setWindowTitle(tr("Data Editor"));
00046 
00047     QToolBar *toolbar;
00048 
00049     toolbar = addToolBar(tr("Playback Toolbar"));
00050     m_playToolbar = toolbar;
00051     toolbar = addToolBar(tr("Play Mode Toolbar"));
00052     
00053     IconLoader il;
00054 
00055     QAction *action = new QAction(il.load("playfollow"), tr("Track Playback"), this);
00056     action->setStatusTip(tr("Toggle tracking of playback position"));
00057     action->setCheckable(true);
00058     action->setChecked(m_trackPlayback);
00059     connect(action, SIGNAL(triggered()), this, SLOT(togglePlayTracking()));
00060     toolbar->addAction(action);
00061 
00062     toolbar = addToolBar(tr("Edit Toolbar"));
00063 
00064     action = new QAction(il.load("datainsert"), tr("Insert New Item"), this);
00065     action->setShortcut(tr("Insert"));
00066     action->setStatusTip(tr("Insert a new item"));
00067     connect(action, SIGNAL(triggered()), this, SLOT(insertRow()));
00068     toolbar->addAction(action);
00069 
00070     action = new QAction(il.load("datadelete"), tr("Delete Selected Items"), this);
00071     action->setShortcut(tr("Delete"));
00072     action->setStatusTip(tr("Delete the selected item or items"));
00073     connect(action, SIGNAL(triggered()), this, SLOT(deleteRows()));
00074     toolbar->addAction(action);
00075 
00076     CommandHistory::getInstance()->registerToolbar(toolbar);
00077 
00078 /*
00079     action = new QAction(il.load("dataedit"), tr("Edit Selected Item"), this);
00080     action->setShortcut(tr("Edit"));
00081     action->setStatusTip(tr("Edit the selected item"));
00082     connect(action, SIGNAL(triggered()), this, SLOT(editRow()));
00083     toolbar->addAction(action);
00084 */
00085 
00086     QFrame *mainFrame = new QFrame;
00087     setCentralWidget(mainFrame);
00088 
00089     QGridLayout *grid = new QGridLayout;
00090     mainFrame->setLayout(grid);
00091     
00092     QGroupBox *box = new QGroupBox;
00093     if (title != "") {
00094         box->setTitle(title);
00095     } else {
00096         box->setTitle(tr("Data in Layer"));
00097     }
00098     grid->addWidget(box, 0, 0);
00099     grid->setRowStretch(0, 15);
00100 
00101     QGridLayout *subgrid = new QGridLayout;
00102     box->setLayout(subgrid);
00103 
00104     subgrid->setSpacing(0);
00105     subgrid->setMargin(5);
00106 
00107     subgrid->addWidget(new QLabel(tr("Find:")), 1, 0);
00108     subgrid->addWidget(new QLabel(tr(" ")), 1, 1);
00109     m_find = new QLineEdit;
00110     subgrid->addWidget(m_find, 1, 2);
00111     connect(m_find, SIGNAL(textChanged(const QString &)),
00112             this, SLOT(searchTextChanged(const QString &)));
00113     connect(m_find, SIGNAL(returnPressed()),
00114             this, SLOT(searchRepeated()));
00115 
00116     m_tableView = new QTableView;
00117     subgrid->addWidget(m_tableView, 0, 0, 1, 3);
00118 
00119     m_tableView->setSortingEnabled(true);
00120     m_tableView->sortByColumn(0, Qt::AscendingOrder);
00121 
00122     m_table = new ModelDataTableModel(model);
00123     m_tableView->setModel(m_table);
00124 
00125     m_tableView->horizontalHeader()->setStretchLastSection(true);
00126 
00127     connect(m_tableView, SIGNAL(clicked(const QModelIndex &)),
00128             this, SLOT(viewClicked(const QModelIndex &)));
00129     connect(m_tableView, SIGNAL(pressed(const QModelIndex &)),
00130             this, SLOT(viewPressed(const QModelIndex &)));
00131     connect(m_tableView->selectionModel(),
00132             SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
00133             this,
00134             SLOT(currentChanged(const QModelIndex &, const QModelIndex &)));
00135     connect(m_table, SIGNAL(addCommand(Command *)),
00136             this, SLOT(addCommand(Command *)));
00137     connect(m_table, SIGNAL(currentChanged(const QModelIndex &)),
00138             this, SLOT(currentChangedThroughResort(const QModelIndex &)));
00139     connect(m_table, SIGNAL(modelRemoved()),
00140             this, SLOT(modelRemoved()));
00141 
00142     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
00143     connect(bb, SIGNAL(rejected()), this, SLOT(close()));
00144     grid->addWidget(bb, 2, 0);
00145     grid->setRowStretch(2, 0);
00146     
00147     QDesktopWidget *desktop = QApplication::desktop();
00148     QRect available = desktop->availableGeometry();
00149 
00150     int width = available.width() / 3;
00151     int height = available.height() / 2;
00152     if (height < 370) {
00153         if (available.height() > 500) height = 370;
00154     }
00155     if (width < 650) {
00156         if (available.width() > 750) width = 650;
00157         else if (width < 500) {
00158             if (available.width() > 650) width = 500;
00159         }
00160     }
00161 
00162     resize(width, height);
00163 }
00164 
00165 ModelDataTableDialog::~ModelDataTableDialog()
00166 {
00167     delete m_table;
00168 }
00169 
00170 void
00171 ModelDataTableDialog::userScrolledToFrame(int frame)
00172 {
00173     QModelIndex index = m_table->getModelIndexForFrame(frame);
00174     makeCurrent(index.row());
00175 }
00176 
00177 void
00178 ModelDataTableDialog::playbackScrolledToFrame(int frame)
00179 {
00180     if (m_trackPlayback) {
00181         QModelIndex index = m_table->getModelIndexForFrame(frame);
00182         makeCurrent(index.row());
00183     }
00184 }
00185 
00186 void
00187 ModelDataTableDialog::searchTextChanged(const QString &text)
00188 {
00189     QModelIndex mi = m_table->findText(text);
00190     if (mi.isValid()) {
00191         makeCurrent(mi.row());
00192         m_tableView->selectionModel()->setCurrentIndex
00193             (mi, QItemSelectionModel::ClearAndSelect);
00194     }
00195 }
00196 
00197 void
00198 ModelDataTableDialog::searchRepeated()
00199 {
00200     QModelIndex mi = m_table->findText(m_find->text());
00201     if (mi.isValid()) {
00202         makeCurrent(mi.row());
00203         m_tableView->selectionModel()->setCurrentIndex
00204             (mi, QItemSelectionModel::ClearAndSelect);
00205     }
00206 }
00207 
00208 void
00209 ModelDataTableDialog::makeCurrent(int row)
00210 {
00211     int rh = m_tableView->height() / m_tableView->rowHeight(0);
00212     int topRow = row - rh/4;
00213     if (topRow < 0) topRow = 0;
00214     
00215     // should only scroll if the desired row is not currently visible
00216 
00217     // should only select if no part of the desired row is currently selected
00218 
00219 //    cerr << "rh = " << rh << ", row = " << row << ", scrolling to "
00220 //              << topRow << endl;
00221 
00222     int pos = m_tableView->rowViewportPosition(row);
00223 
00224     if (pos < 0 || pos >= m_tableView->height() - rh) {
00225         m_tableView->scrollTo(m_table->index(topRow, 0));
00226     }
00227 
00228     bool haveRowSelected = false;
00229     for (int i = 0; i < m_table->columnCount(); ++i) {
00230         if (m_tableView->selectionModel()->isSelected(m_table->index(row, i))) {
00231             haveRowSelected = true;
00232             break;
00233         }
00234     }
00235 
00236     if (!haveRowSelected) {
00237         m_tableView->selectionModel()->setCurrentIndex
00238             (m_table->index(row, 0),
00239              QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
00240     }
00241 }
00242 
00243 void
00244 ModelDataTableDialog::viewClicked(const QModelIndex &index)
00245 {
00246 //    SVDEBUG << "ModelDataTableDialog::viewClicked: " << index.row() << ", " << index.column() << endl;
00247     emit scrollToFrame(m_table->getFrameForModelIndex(index));
00248 }
00249 
00250 void
00251 ModelDataTableDialog::viewPressed(const QModelIndex &)
00252 {
00253 //    SVDEBUG << "ModelDataTableDialog::viewPressed: " << index.row() << ", " << index.column() << endl;
00254 }
00255 
00256 void
00257 ModelDataTableDialog::currentChanged(const QModelIndex &current,
00258                                      const QModelIndex &)
00259 {
00260 //    SVDEBUG << "ModelDataTableDialog::currentChanged: from "
00261 //              << previous.row() << ", " << previous.column()
00262 //              << " to " << current.row() << ", " << current.column() 
00263 //              << endl;
00264     m_currentRow = current.row();
00265     m_table->setCurrentRow(m_currentRow);
00266 }
00267 
00268 void
00269 ModelDataTableDialog::insertRow()
00270 {
00271     m_table->insertRow(m_currentRow);
00272 }
00273 
00274 void
00275 ModelDataTableDialog::deleteRows()
00276 {
00277     // not efficient
00278     while (m_tableView->selectionModel()->hasSelection()) {
00279         m_table->removeRow
00280             (m_tableView->selectionModel()->selection().indexes().begin()->row());
00281     }
00282 }
00283 
00284 void
00285 ModelDataTableDialog::editRow()
00286 {
00287 }
00288 
00289 void
00290 ModelDataTableDialog::addCommand(Command *command)
00291 {
00292     CommandHistory::getInstance()->addCommand(command, false, true);
00293 }
00294 
00295 void
00296 ModelDataTableDialog::togglePlayTracking()
00297 {
00298     m_trackPlayback = !m_trackPlayback;
00299 }
00300 
00301 void
00302 ModelDataTableDialog::currentChangedThroughResort(const QModelIndex &index)
00303 {
00304 //    SVDEBUG << "ModelDataTableDialog::currentChangedThroughResort: row = " << index.row() << endl;
00305 //  m_tableView->scrollTo(index);
00306     makeCurrent(index.row());
00307 }
00308 
00309 void
00310 ModelDataTableDialog::modelRemoved()
00311 {
00312     close();
00313 }
00314 
00315