1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <QApplication>
#include <QHeaderView>
#include <QKeyEvent>
#include <QMenu>
#include "chartablemodel.h"
#include "chartableview.h"
#include "charzoom.h"
#include "fonts/scface.h"
CharTableView::CharTableView(QWidget * parent)
: QTableView(parent),
zoom(0)
{
deleteAct = new QAction( tr("Delete"), this);
connect(deleteAct, SIGNAL(triggered()), this, SLOT(removeCharacter()));
connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
this, SLOT(viewDoubleClicked(const QModelIndex &)));
actionMenu = new QMenu(this);
actionMenu->addAction(deleteAct);
setAcceptDrops(false);
setDropIndicatorShown(true);
verticalHeader()->setVisible(false);
horizontalHeader()->setVisible(false);
}
CharTableModel * CharTableView::model()
{
return qobject_cast<CharTableModel*>(QTableView::model());
}
void CharTableView::removeCharacter()
{
model()->removeCharacter(deleteAct->data().toInt());
}
void CharTableView::modelSelectionChanged(QItemSelectionModel * model)
{
setSelectionModel(model);
}
void CharTableView::resizeLastRow()
{
setRowHeight(model()->rowCount()-1, width() / model()->columnCount() + 5);
}
void CharTableView::keyPressEvent(QKeyEvent *k)
{
switch (k->key())
{
case Qt::Key_Backspace:
case Qt::Key_Delete:
emit delChar();
break;
case Qt::Key_Insert:
// safely emit selectChar(model()->characters()[currentCharactersIndex()]);
viewDoubleClicked(QModelIndex());
break;
}
QTableView::keyPressEvent(k);
}
void CharTableView::mousePressEvent(QMouseEvent* e)
{
QTableView::mousePressEvent(e);
int index = currentCharactersIndex();
if (index < 0)
return;
int currentChar = -1;
if ((index < model()->characters().count()) && (model()->characters().count() > 0))
currentChar = model()->characters()[index];
if (e->button() == Qt::RightButton && currentChar > -1)
{
// Only non-dropable tables show "magnifier glass"
if (!acceptDrops())
{
hideZoomedChar();
zoom = new CharZoom(this, currentChar, model()->fontFace());
zoom->move(e->globalPos().x()-2, e->globalPos().y()-2);
zoom->show();
}
else
{
deleteAct->setData(index);
actionMenu->popup(e->globalPos());
}
}
}
void CharTableView::mouseMoveEvent(QMouseEvent* e)
{
// HACK to prevent strange Qt4 cursor behaviour after dropping. It's examined by Trolltech now - PV.
// It's the one and only reason why to include QApplication here.
// Fixed at least in Qt-4.4.2
// QApplication::restoreOverrideCursor();
hideZoomedChar();
QTableView::mouseMoveEvent(e);
}
void CharTableView::mouseReleaseEvent(QMouseEvent* e)
{
hideZoomedChar();
QTableView::mouseReleaseEvent(e);
}
void CharTableView::viewDoubleClicked(const QModelIndex & /*index*/)
{
int charIndex = currentCharactersIndex();
if (model()->characters().count() > charIndex)
emit selectChar(model()->characters()[charIndex], model()->fonts()[charIndex]);
}
int CharTableView::currentCharactersIndex()
{
QModelIndex index = currentIndex();
if (!index.isValid())
return -1;
return index.row() * model()->columnCount() + index.column();
}
void CharTableView::hideZoomedChar()
{
if (zoom)
{
zoom->close();
delete zoom;
zoom = 0;
}
}
void CharTableView::hideEvent(QHideEvent * e)
{
hideZoomedChar();
QTableView::hideEvent(e);
}
void CharTableView::resizeEvent(QResizeEvent *e)
{
QTableView::resizeEvent(e);
if (model())
{
model()->setViewWidth(e->size().width());
// The resizeColumnsToContents() method won't work here.
// It doesn't handle cells without any content. And it creates
// larger columns than required. Dunno why.
for (int i = 0; i < model()->columnCount(); ++i)
setColumnWidth(i, e->size().width() / model()->columnCount());
for (int i = 0; i < model()->rowCount(); ++i)
setRowHeight(i, e->size().width() / model()->columnCount() + 5);
}
}
|