Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

chartableview.cpp

Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include <QApplication>
00008 #include <QHeaderView>
00009 #include <QKeyEvent>
00010 #include <QMenu>
00011 
00012 #include "chartablemodel.h"
00013 #include "chartableview.h"
00014 #include "charzoom.h"
00015 #include "fonts/scface.h"
00016 
00017 
00018 CharTableView::CharTableView(QWidget * parent)
00019     : QTableView(parent),
00020     zoom(0)
00021 {
00022     deleteAct = new QAction( tr("Delete"), this);
00023     connect(deleteAct, SIGNAL(triggered()), this, SLOT(removeCharacter()));
00024     connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
00025             this, SLOT(viewDoubleClicked(const QModelIndex &)));
00026     actionMenu = new QMenu(this);
00027     actionMenu->addAction(deleteAct);
00028 
00029     setAcceptDrops(false);
00030     setDropIndicatorShown(true);
00031 
00032     verticalHeader()->setVisible(false);
00033     horizontalHeader()->setVisible(false);
00034 }
00035 
00036 CharTableModel * CharTableView::model()
00037 {
00038     return qobject_cast<CharTableModel*>(QTableView::model());
00039 }
00040 
00041 void CharTableView::removeCharacter()
00042 {
00043     model()->removeCharacter(deleteAct->data().toInt());
00044 }
00045 
00046 void CharTableView::modelSelectionChanged(QItemSelectionModel * model)
00047 {
00048     setSelectionModel(model);
00049 }
00050 
00051 void CharTableView::keyPressEvent(QKeyEvent *k)
00052 {
00053     switch (k->key())
00054     {
00055         case Qt::Key_Backspace:
00056         case Qt::Key_Delete:
00057             emit delChar();
00058             break;
00059         case Qt::Key_Insert:
00060             // safely emit selectChar(model()->characters()[currenCharactersIndex()]);
00061             viewDoubleClicked(QModelIndex());
00062             break;
00063     }
00064     QTableView::keyPressEvent(k);
00065 }
00066 
00067 void CharTableView::mousePressEvent(QMouseEvent* e)
00068 {
00069     QTableView::mousePressEvent(e);
00070 
00071     int index = currenCharactersIndex();
00072     int currentChar = -1;
00073 
00074     if (index < model()->characters().count())
00075         currentChar = model()->characters()[index];
00076 
00077     if (e->button() == Qt::RightButton && currentChar > -1)
00078     {
00079         // Only non-dropable tables show "magnifier glass"
00080         if (!acceptDrops())
00081         {
00082             hideZoomedChar();
00083             zoom = new CharZoom(this, currentChar, model()->fontFace());
00084             zoom->move(e->globalPos().x()-2, e->globalPos().y()-2);
00085             zoom->show();
00086         }
00087         else
00088         {
00089             deleteAct->setData(index);
00090             actionMenu->popup(e->globalPos());
00091         }
00092     }
00093 }
00094 
00095 void CharTableView::mouseMoveEvent(QMouseEvent* e)
00096 {
00097     // HACK to prevent strange Qt4 cursor behaviour after dropping. It's examined by Trolltech now - PV.
00098     // It's the one and only reason why to include QApplication here.
00099     QApplication::restoreOverrideCursor();
00100     hideZoomedChar();
00101     QTableView::mouseMoveEvent(e);
00102 }
00103 
00104 void CharTableView::mouseReleaseEvent(QMouseEvent* e)
00105 {
00106     hideZoomedChar();
00107     QTableView::mouseReleaseEvent(e);
00108 }
00109 
00110 void CharTableView::viewDoubleClicked(const QModelIndex & /*index*/)
00111 {
00112     if (model()->characters().count() > currenCharactersIndex())
00113         emit selectChar(model()->characters()[currenCharactersIndex()]);
00114 }
00115 
00116 int CharTableView::currenCharactersIndex()
00117 {
00118     return currentIndex().row() * model()->columnCount() + currentIndex().column();
00119 }
00120 
00121 void CharTableView::hideZoomedChar()
00122 {
00123     if (zoom)
00124     {
00125         zoom->close();
00126         delete zoom;
00127         zoom = 0;
00128     }
00129 }
00130 
00131 void CharTableView::hideEvent(QHideEvent * e)
00132 {
00133     hideZoomedChar();
00134     QTableView::hideEvent(e);
00135 }
00136 
00137 void CharTableView::resizeEvent(QResizeEvent *e)
00138 {
00139     QTableView::resizeEvent(e);
00140     if (model())
00141     {
00142         model()->setViewWidth(e->size().width());
00143         resizeRowsToContents();
00144         // The resizeColumnsToContents() method won't work here.
00145         // It doesn't handle cells without any content. And it creates
00146         // larger columns than required. Dunno why.
00147         for (int i = 0; i < model()->columnCount(); ++i)
00148             setColumnWidth(i, e->size().width() / model()->columnCount());
00149     }
00150 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.