Back to index

scribus-ng  1.3.4.dfsg+svn20071115
chartable.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 
00008 #include <qtable.h>
00009 //#include <qtimer.h>
00010 #include <qdragobject.h>
00011 
00012 #include "scribuscore.h"
00013 #include "scribusdoc.h"
00014 #include "scribusview.h"
00015 
00016 #include "chartable.h"
00017 #include "chartable.moc"
00018 #include "charzoom.h"
00019 
00020 
00021 CharTable::CharTable(QWidget* parent, int cols, ScribusDoc* doc, QString font)
00022        : QTable(parent),
00023        mPressed(false),
00024        zoom(0),
00025 //     m_Item(pi),
00026        m_fontInUse(font)
00027 {
00028        m_characters.clear();
00029        //watchTimer = new QTimer(this);
00030 //     connect(watchTimer, SIGNAL(timeout()), this, SLOT(showAlternate()));
00031 //     alternate = false;
00032        // gui
00033        m_doc = doc;
00034        setNumCols(cols);
00035        setLeftMargin(0);
00036        verticalHeader()->hide();
00037        setTopMargin(0);
00038        horizontalHeader()->hide();
00039        setSorting(false);
00040        setSelectionMode(QTable::NoSelection);
00041        setColumnMovingEnabled(false);
00042        setRowMovingEnabled(false);
00043        setReadOnly(true);
00044        setDragEnabled(true);
00045        enableDrops(true);
00046 
00047        connect(this, SIGNAL(dropped(QDropEvent *)), this, SLOT(slotDropped(QDropEvent *)));
00048 }
00049 
00050 void CharTable::setDoc(ScribusDoc *doc)
00051 {
00052        bool repaint = (doc == m_doc) ? false : true;
00053 
00054        m_doc = doc;
00055        if (repaint)
00056               recalcCellSizes();
00057 }
00058 
00059 QRect CharTable::cellGeometry ( int /*row*/, int /*col*/ ) const
00060 {
00061        if (!m_doc || m_fontInUse.isEmpty())
00062               return QRect(0, 0, 1, 1);
00063 
00064        int widthHeight = QMAX(18 + qRound(-(*m_doc->AllFonts)[m_fontInUse].descent() * 18) + 5, 18);
00065        return QRect(0, 0, widthHeight, widthHeight+20);
00066 }
00067 
00068 
00069 void CharTable::paintCell( QPainter * qp, int row, int col, const QRect & cr, bool /*selected*/, const QColorGroup & /*cg*/ )
00070 {
00071        if (!m_doc)
00072               return;
00073 
00074        static QPixmap pixm;
00075 
00076        uint cc = row * numCols() + col;
00077        if (cc >= m_characters.count())
00078               return;
00079 
00080        QRect sz = cellGeometry(row, col);
00081        pixm.resize(sz.width(), sz.height());
00082 
00083        ScPainter *p = new ScPainter(&pixm, cr.width(), cr.height());
00084        p->clear();
00085        pixm.fill(white);
00086        QWMatrix chma;
00087        chma.scale(1.6, 1.6);
00088        qp->eraseRect(0, 0, cr.width(), cr.height());
00089        QFont fo = qp->font();
00090        fo.setPixelSize(9);
00091        qp->setFont(fo);
00092        static FPointArray gly;
00093        ScFace face = (*m_doc->AllFonts)[m_fontInUse];
00094        uint gl = face.char2CMap(m_characters[cc]);
00095        gly = face.glyphOutline(gl);
00096        if (gly.size() > 4)
00097        {
00098               gly.map(chma);
00099               double ww = sz.width() - face.glyphWidth(gl)*numCols();
00100               p->translate(ww / 2, 1);
00101               p->setBrush(black);
00102               p->setFillMode(1);
00103               p->setupPolygon(&gly);
00104               p->fillPath();
00105               p->end();
00106               int x = QMAX(0, (cr.width() - sz.width()) / 2);
00107               qp->drawPixmap(x, 1, pixm);
00108               QString tmp;
00109               tmp.sprintf("%04X", m_characters[row*numCols()+col]);
00110               qp->setPen(black);
00111               qp->drawText(QRect(2, cr.height()-10, cr.width()-4, 9),Qt::AlignCenter, tmp);
00112        }
00113        qp->setPen(gray);
00114        qp->drawRect(0, 0, cr.width(), cr.height());
00115        delete p;
00116 }
00117 
00118 
00119 void CharTable::keyPressEvent(QKeyEvent *k)
00120 {
00121        switch (k->key())
00122        {
00123               case Key_Backspace:
00124               case Key_Delete:
00125                      emit delChar();
00126                      break;
00127               case Key_Insert:
00128                      //emit selectChar(currentRow(), currentColumn());
00129                      emit selectChar(m_characters[currentRow() * numCols() + currentColumn()]);
00130                      break;
00131        }
00132        QTable::keyPressEvent(k);
00133 }
00134 
00135 void CharTable::contentsMousePressEvent(QMouseEvent* e)
00136 {
00137        //e->accept();
00138        uint r = rowAt(e->pos().y());
00139        uint c = columnAt(e->pos().x());
00140 
00141        mPressed = true;
00142        m_mousePosition = QCursor::pos();
00143        uint index = r * numCols() + c;
00144        int currentChar = -1;
00145        if (index < m_characters.count())
00146               currentChar = m_characters[index];
00147 
00148        if (e->button() == RightButton && currentChar > -1)
00149        {
00150               //watchTimer->stop();
00151               // Only non-dropable tables show "magnifier glass"
00152               if (!viewport()->acceptDrops())
00153               {
00154                      zoom = new CharZoom(this, currentChar, (*m_doc->AllFonts)[m_fontInUse]);
00155                      zoom->move(m_mousePosition.x()-2, m_mousePosition.y()-2);
00156                      zoom->show();
00157               }
00158               else
00159               {
00160                      // delete popup menu when it accepts drops
00161                      QPopupMenu *pmen = new QPopupMenu();
00162                      int pid = pmen->insertItem( tr("Delete"), this, SLOT(deleteOwnCharacter(int)));
00163                      pmen->setItemParameter(pid, index);
00164                      pmen->exec(QCursor::pos());
00165                      delete pmen;
00166               }
00167        }
00168        if (e->button() == LeftButton)
00169        {
00170               selectCells(r, c, r, c);
00171               cCol = c;
00172               cRow = r;
00173        }
00174        QTable::contentsMousePressEvent(e);
00175 }
00176 
00177 void CharTable::deleteOwnCharacter(int index)
00178 {
00179        if (m_characters.remove(m_characters[index]) > 0)
00180        {
00181               recalcCellSizes();
00182               // WTF? why it does not repaint?
00183               repaint(0, 0, width(), height(), true);
00184        }
00185        else
00186               qDebug("CharTable::deleteOwnCharacter: no char deleted - logical error propably");
00187 }
00188 
00189 void CharTable::contentsMouseReleaseEvent(QMouseEvent* e)
00190 {
00191        e->accept();
00192        //watchTimer->stop();
00193        if ((e->button() == RightButton) && (mPressed))
00194        {
00195               if (zoom)
00196               {
00197                      zoom->close();
00198                      delete zoom;
00199                      zoom = 0;
00200               }
00201        }
00202        //if ((e->button() == LeftButton) && (!alternate))
00203        if (e->button() == LeftButton)
00204        {
00205               int index = rowAt(e->pos().y()) * numCols() + columnAt(e->pos().x());
00206               if (index >= 0)
00207                      emit selectChar(m_characters[index]);
00208        }
00209        mPressed = false;
00210 //     alternate = false;
00211        QTable::contentsMouseReleaseEvent(e);
00212 }
00213 
00214 void CharTable::showAlternate()
00215 {
00216 /*     watchTimer->stop();
00217        alternate = true;
00218        QString font;
00219        QString chToIns = "";
00220        font = par->fontInUse;
00221        uint baseChar = rowA*16+colA;
00222        if (baseChar < maxCount)
00223        {
00224               QPopupMenu *pmen = new QPopupMenu();
00225               chToIns = QChar(par->characters[baseChar]);
00226               pmen->insertItem(FontSample((*m_Item->doc()->AllFonts)[font], 20, chToIns, paletteBackgroundColor(), true));
00227               if ((*m_Item->doc()->AllFonts)[font]->CharWidth.contains(par->characters[baseChar] + 0xF720))
00228               {
00229                      chToIns = QChar(par->characters[baseChar] + 0xF720);
00230                      pmen->insertItem(FontSample((*m_Item->doc()->AllFonts)[font], 20, chToIns, paletteBackgroundColor(), true));
00231               }
00232               int re = pmen->indexOf(pmen->exec(QCursor::pos()));
00233               delete pmen;
00234        } */
00235 }
00236 
00237 void CharTable::recalcCellSizes()
00238 {
00239        int ab = m_characters.count() / numCols();
00240        if (m_characters.count() % numCols() != 0)
00241               ab++;
00242        setNumRows(ab);
00243 
00244        int cellWidth = width() / numCols();
00245        int cellHeight = cellWidth;
00246        for (int d = 0; d < numCols(); ++d)
00247               setColumnStretchable(d, TRUE);
00248        for (int d = 0; d < numRows(); ++d)
00249               setRowHeight(d, cellHeight);
00250        updateScrollBars();
00251 }
00252 
00253 void CharTable::setCharacters(CharClassDef ch)
00254 {
00255        m_characters.clear();
00256        m_characters = ch;
00257        recalcCellSizes();
00258 }
00259 
00260 void CharTable::setFontInUse(QString font)
00261 {
00262        bool repaint = (font == m_fontInUse) ? false : true;
00263        if (repaint)
00264        {
00265               m_fontInUse = font;
00266               recalcCellSizes();
00267        }
00268 }
00269 
00270 void CharTable::enableDrops(bool e)
00271 {
00272        viewport()->setAcceptDrops(e);
00273 }
00274 
00275 QDragObject * CharTable::dragObject()
00276 {
00277        QString s("%1");
00278        uint val = cRow * numCols() + cCol;
00279        return new QTextDrag(s.arg(m_characters[val]), this);
00280 }
00281 
00282 void CharTable::slotDropped(QDropEvent *evt)
00283 {
00284        if (evt->source() == this)
00285               return;
00286 
00287        QString label;
00288        if ( QTextDrag::decode(evt, label))
00289               appendUnicode(label, 10);
00290 }
00291 
00292 void CharTable::appendUnicode(QString s, uint base)
00293 {
00294        bool ok;
00295        int val = s.toInt(&ok, base);
00296        if (ok && !m_characters.contains(val))
00297        {
00298               m_characters.append(val);
00299               recalcCellSizes();
00300        }
00301 }