kexi

kexirecordmarker.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
00003    Daniel Molkentin <molkentin@kde.org>
00004    Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 
00021    Original Author:  Till Busch <till@bux.at>
00022    Original Project: buX (www.bux.at)
00023 */
00024 
00025 #include "kexirecordmarker.h"
00026 
00027 #include <qcolor.h>
00028 #include <qstyle.h>
00029 #include <qpixmap.h>
00030 #include <qpainter.h>
00031 #include <qimage.h>
00032 
00033 #include <kdebug.h>
00034 #include <kstaticdeleter.h>
00035 
00036 static KStaticDeleter<QImage> KexiRecordMarker_pen_deleter, KexiRecordMarker_plus_deleter;
00037 QImage* KexiRecordMarker_pen = 0, *KexiRecordMarker_plus = 0;
00038 
00039 static const unsigned char img_pen_data[] = {
00040     0x00,0x00,0x03,0x30,0x78,0x9c,0xfb,0xff,0xff,0x3f,0xc3,0x7f,0x32,0x30,
00041     0x10,0x80,0x88,0xff,0xe4,0xe8,0x85,0xe9,0xc7,0xc6,0x26,0x55,0x3f,0x3a,
00042     0x4d,0x8e,0x7e,0x72,0xfc,0x32,0xd2,0xf5,0xa3,0xeb,0xa5,0xb5,0x7e,0x5c,
00043     0xe9,0x85,0x54,0xfb,0xb1,0xa5,0x1b,0x52,0xdc,0x0e,0x00,0xf2,0xea,0x0a,
00044     0x13
00045 };
00046 static const unsigned char img_plus_data[] = {
00047     0x00,0x00,0x01,0x90,0x78,0x9c,0xfb,0xff,0xff,0x3f,0xc3,0x7f,0x28,0x86,
00048     0x82,0xff,0x50,0x0c,0x17,0x47,0xc7,0xd4,0x50,0x87,0x05,0xc0,0xd5,0xe1,
00049     0x10,0xa7,0x16,0x26,0xca,0x5e,0x7c,0xfe,0x20,0x47,0x1d,0xb2,0x5a,0x5c,
00050     0xea,0x40,0x72,0x00,0x03,0x6e,0x74,0x8c
00051 };
00052 
00053 static struct EmbedImage {
00054     int width, height, depth;
00055     const unsigned char *data;
00056     ulong compressed;
00057     int numColors;
00058     const QRgb *colorTable;
00059     bool alpha;
00060     const char *name;
00061 } embed_image[] = {
00062     { 17, 12, 32, (const unsigned char*)img_pen_data, 57, 0, 0, true, "tableview_pen.png" },
00063     { 10, 10, 32, (const unsigned char*)img_pen_data, 50, 0, 0, true, "tableview_plus.png" }
00064 };
00065 
00066 QImage* getImg(const unsigned char* data, int id)
00067 {
00068     QByteArray baunzip;
00069     baunzip = qUncompress( data, embed_image[id].compressed );
00070     QImage *img = new QImage( QImage((uchar*)baunzip.data(),
00071             embed_image[id].width, embed_image[id].height,
00072             embed_image[id].depth, (QRgb*)embed_image[id].colorTable,
00073             embed_image[id].numColors, QImage::BigEndian
00074     ).copy() );
00075     if ( embed_image[id].alpha )
00076         img->setAlphaBuffer(true);
00077     return img;
00078 }
00079 
00080 static void initRecordMarkerImages()
00081 {
00082     if (!KexiRecordMarker_pen) {
00084         KexiRecordMarker_pen_deleter.setObject( KexiRecordMarker_pen, getImg(img_pen_data, 0) );
00085         KexiRecordMarker_plus_deleter.setObject( KexiRecordMarker_plus, getImg(img_plus_data, 1) );
00086     }
00087 }
00088 
00089 KexiRecordMarker::KexiRecordMarker(QWidget *parent, const char* name)
00090 :QWidget(parent, name)
00091 {
00092     m_rowHeight = 1;
00093     m_offset=0;
00094     m_currentRow=-1;
00095     m_editRow=-1;
00096     m_pointerColor = QColor(99,0,0);
00097     m_rows = 0;
00098     m_showInsertRow = true;//false;
00099     
00100 //  getImg(m_penImg, img_pen_data, 0);
00101 //  getImg(m_plusImg, img_plus_data, 1);
00102     initRecordMarkerImages();
00103 }
00104 
00105 QImage* KexiRecordMarker::penImage()
00106 {
00107     initRecordMarkerImages();
00108     return KexiRecordMarker_pen;
00109 }
00110 
00111 QImage* KexiRecordMarker::plusImage()
00112 {
00113     initRecordMarkerImages();
00114     return KexiRecordMarker_plus;
00115 }
00116 
00117 KexiRecordMarker::~KexiRecordMarker()
00118 {
00119 }
00120 
00121 void KexiRecordMarker::addLabel(bool upd)
00122 {
00123     m_rows++;
00124     if (upd)
00125         update();
00126 }
00127 
00128 void KexiRecordMarker::removeLabel(bool upd)
00129 {
00130     if (m_rows > 0) {
00131         m_rows--;
00132         if (upd)
00133             update();
00134     }
00135 }
00136 
00137 void KexiRecordMarker::addLabels(int num, bool upd)
00138 {
00139     m_rows += num;
00140     if (upd)
00141         update();
00142 }
00143 
00144 void KexiRecordMarker::clear(bool upd)
00145 {
00146     m_rows=0;
00147     if (upd)
00148         update();
00149 }
00150 
00151 int KexiRecordMarker::rows() const
00152 {
00153     if (m_showInsertRow)
00154         return m_rows +1;
00155     else
00156         return m_rows;
00157 }
00158 
00159 void KexiRecordMarker::paintEvent(QPaintEvent *e)
00160 {
00161     QPainter p(this);
00162     QRect r(e->rect());
00163 
00164     int first = (r.top()    + m_offset) / m_rowHeight;
00165     int last  = (r.bottom() + m_offset) / m_rowHeight;
00166     if(last > (m_rows-1+(m_showInsertRow?1:0)))
00167         last = m_rows-1+(m_showInsertRow?1:0);
00168 
00169     for(int i=first; i <= last; i++)
00170     {
00171         int y = ((m_rowHeight * i)-m_offset);
00172         QRect r(0, y, width(), m_rowHeight);
00173         p.drawRect(r);
00174         style().drawPrimitive( QStyle::PE_HeaderSection, &p, r,
00175             colorGroup(), QStyle::Style_Raised |
00176             (isEnabled() ? QStyle::Style_Enabled : 0));
00177     }
00178     if (m_editRow!=-1 && m_editRow >= first && m_editRow <= (last/*+1 for insert row*/)) {
00179         //show pen when editing
00180         int ofs = m_rowHeight / 4;
00181         int pos = ((m_rowHeight*(m_currentRow>=0?m_currentRow:0))-m_offset)-ofs/2+1;
00182         p.drawImage((m_rowHeight-KexiRecordMarker_pen->width())/2,
00183             (m_rowHeight-KexiRecordMarker_pen->height())/2+pos,*KexiRecordMarker_pen);
00184     }
00185     else if (m_currentRow >= first && m_currentRow <= last 
00186         && (!m_showInsertRow || (m_showInsertRow && m_currentRow < last)))/*don't display marker for 'insert' row*/ 
00187     {
00188         //show marker
00189         p.setBrush(colorGroup().foreground());
00190         QPointArray points(3);
00191         int ofs = m_rowHeight / 4;
00192         int ofs2 = (width() - ofs) / 2 -1;
00193         int pos = ((m_rowHeight*m_currentRow)-m_offset)-ofs/2+2;
00194         points.putPoints(0, 3, ofs2, pos+ofs, ofs2 + ofs, pos+ofs*2, 
00195             ofs2,pos+ofs*3);
00196         p.drawPolygon(points);
00197 //      kdDebug() <<"KexiRecordMarker::paintEvent(): POLYGON" << endl;
00198 /*      int half = m_rowHeight / 2;
00199         points.setPoints(3, 2, pos + 2, width() - 5, pos + half, 2, pos + (2 * half) - 2);*/
00200     }
00201     if (m_showInsertRow && m_editRow < last
00202         && last == (m_rows-1+(m_showInsertRow?1:0)) ) {
00203         //show plus sign
00204         int pos = ((m_rowHeight*last)-m_offset)+(m_rowHeight-KexiRecordMarker_plus->height())/2;
00205 //      p.drawImage((width()-m_plusImg.width())/2-1, pos, m_plusImg);
00206         p.drawImage((width()-KexiRecordMarker_plus->width())/2, pos, *KexiRecordMarker_plus);
00207     }
00208 }
00209 
00210 void KexiRecordMarker::setCurrentRow(int row)
00211 {
00212     int oldRow = m_currentRow;
00213     m_currentRow=row;
00214     
00215     update(0,(m_rowHeight*(oldRow))-m_offset-1, width()+2, m_rowHeight+2);
00216     update(0,(m_rowHeight*row)-m_offset-1, width()+2, m_rowHeight+2);
00217 }
00218 
00219 void KexiRecordMarker::setOffset(int offset)
00220 {
00221     int oldOff = m_offset;
00222     m_offset = offset;
00223     scroll(0,oldOff-offset);
00224 }
00225 
00226 void KexiRecordMarker::setCellHeight(int cellHeight)
00227 {
00228     m_rowHeight = cellHeight;
00229 }
00230 
00231 void KexiRecordMarker::setEditRow(int row)
00232 {
00233     m_editRow = row;
00234 //TODO: update only needed area!
00235     update();
00236 }
00237 
00238 void KexiRecordMarker::showInsertRow(bool show)
00239 {
00240     m_showInsertRow = show;
00241 //TODO: update only needed area!
00242     update();
00243 }
00244 
00245 void KexiRecordMarker::setColor(const QColor &newcolor)
00246 {
00247     m_pointerColor = newcolor;
00248 }
00249 
00250 #include "kexirecordmarker.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys