📄 ktablistbox.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 1997 The KDE Team This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/// Written by Stefan Taferner <taferner@kde.org>// Modified by Alessandro Russo <axela@bigfoot.org>//// BugFix:// -The resize cursor appared in the wrong position when the tab is// scrolled to the right.// -Sometimes some columns became suddenly pressed.// -Corrected resizing of the last column for fill the real size of the table.//// New Features:// -Drag&Slide of the columns bar like Window95.// -Complete key Bindings (Must be enable by enableKey(). )// -Three Mode to Order the content of table// (this mode are column specific, but you should'nt mix SimpleOrder with// ComplexOrder for obvious reason):// NoOrder = Old mode, do not order.// SimpleOrder = An arrow appear near the title of column, are all// disabled except the current selected column.// ComplexOrder= Use a new widget KNumCheckButton, the behaviour is very// complex:// Single Left Click : set to the next free number if the// button is blank,or else do nothing.// Single Right Click: clear the button and adjust the others.// Double Left Click : clear all buttons and set the current// to '1'.// Double Right Click: clear all buttons.////// Todo:// - Save the current status of checkbutton/arrows to the config file.#include "ktablistbox.h"#include <qfontmetrics.h>#include <qpainter.h>#include <qkeycode.h>#include <qbitmap.h>#include <qdrawutil.h>#include <qscrollbar.h>#include <kapp.h>// This is only for flushKeys().#include <X11/Xlib.h>#include <stdarg.h>#define INIT_MAX_ITEMS 16#define ARROW_SPACE 15#define BUTTON_SPACE 4#define MINIMUM_SPACE 9#include "ktablistbox.h"//=============================================================================//// C L A S S KTabListBoxItem////=============================================================================KTabListBoxItem::KTabListBoxItem(int aColumns){ columns = aColumns; txt = new QString[columns]; mark = -2;}//-----------------------------------------------------------------------------KTabListBoxItem::~KTabListBoxItem(){ if (txt) delete[] txt; txt = 0L;}//-----------------------------------------------------------------------------KTabListBoxItem& KTabListBoxItem::operator=(const KTabListBoxItem& from){ int i; for (i=0; i<columns; i++) txt[i] = from.txt[i]; fgColor = from.fgColor; return *this;}//=============================================================================//// C L A S S KTabListBoxColumn////=============================================================================//-----------------------------------------------------------------------------KTabListBoxColumn::KTabListBoxColumn(KTabListBox* pa, int w): QObject(){ initMetaObject(); iwidth = w; idefwidth = w; colType = KTabListBox::TextColumn; ordmode = KTabListBox::Descending; ordtype = KTabListBox::NoOrder; parent = pa; inumber=0; vline=false; mbut=0L;}//-----------------------------------------------------------------------------KTabListBoxColumn::~KTabListBoxColumn(){ if(mbut) delete mbut;}//-----------------------------------------------------------------------------bool KTabListBoxColumn::changeMode(){ if(inumber>0) { if(ordmode==KTabListBox::Ascending) ordmode=KTabListBox::Descending; else ordmode=KTabListBox::Ascending; return true; } else if(ordtype==KTabListBox::SimpleOrder) { parent->clearAllNum(); inumber=1; return true; } return false;}//-----------------------------------------------------------------------------void KTabListBoxColumn::setNumber(int num){ bool changed=false; if((inumber==0 && num!=0) || (inumber!=0 && num==0) ) changed=true; if(num>9) num=0; inumber=num; char tmp[2]; tmp[0]=48+inumber; tmp[1]=0; if(!inumber) tmp[0]=' '; if(mbut) mbut->setText(tmp); if(changed) parent->reorderRows();}//-----------------------------------------------------------------------------void KTabListBoxColumn::setOrder(KTabListBox::OrderType ort, KTabListBox::OrderMode mode){ ordtype = ort; ordmode = mode; if(ordtype==KTabListBox::ComplexOrder && !mbut) { mbut = new KNumCheckButton(parent); connect(mbut,SIGNAL(selected() ),this,SLOT(setButton() ) ); connect(mbut,SIGNAL(deselected() ),this,SLOT(resetButton() ) ); connect(mbut,SIGNAL(doubleclick(bool)),this,SLOT(clearAll(bool) ) ); }}//-----------------------------------------------------------------------------void KTabListBoxColumn::paintCell(QPainter* paint, int row, const QString& string, bool marked){ QFontMetrics fm = paint->fontMetrics(); QPixmap* pix = 0L; int beg, end, x; QPen pen, oldPen; if (marked) { paint->fillRect(0, 0, iwidth, parent->cellHeight(row), parent->highlightColor); pen.setColor(kapp->selectTextColor); oldPen = paint->pen(); paint->setPen(pen); } if (!string.isEmpty()) switch(colType) { case KTabListBox::PixmapColumn: if (string) pix = parent->dict().find(string); if (pix && !pix->isNull()) { paint->drawPixmap(0, 0, *pix); break; } /*else output as string*/ case KTabListBox::TextColumn: paint->drawText(1, fm.ascent() +(fm.leading()), (const char*)string); break; case KTabListBox::MixedColumn: QString pixName; for (x=0, beg=0; string[beg] == '\t'; x+=parent->tabPixels, beg++) ; end = beg-1; while(string[beg] == '{') { end = string.find('}', beg+1); if (end >= 0) { pixName = string.mid(beg+1, end-beg-1); pix = parent->dict().find(pixName); if (!pix) { debug("KTabListBox "+QString(name())+ ":\nno pixmap for\n`"+pixName+"' registered."); } if (!pix->isNull()) paint->drawPixmap(x, 0, *pix); x += pix->width()+1; beg = end+1; } else break; } paint->drawText(x+1, fm.ascent() +(fm.leading()), (const char*)string.mid(beg, string.length()-beg)); break; } if (marked) { paint->fillRect(iwidth-6, 0, iwidth, 128, parent->highlightColor); paint->setPen(oldPen); } else paint->eraseRect(iwidth-6, 0, iwidth, 128);}//-----------------------------------------------------------------------------void KTabListBoxColumn::paint(QPainter* paint){ int t=3; QPoint a,b(0,0); QFontMetrics fm = paint->fontMetrics(); if(ordtype!=KTabListBox::NoOrder) { t+=15; if(inumber>0) if(ordmode==KTabListBox::Ascending) paint->drawPixmap(-3, -2, parent->upPix); else paint->drawPixmap(-3, -1, parent->downPix); else if(ordmode==KTabListBox::Ascending) paint->drawPixmap(-3, -2, parent->disabledUpPix); else paint->drawPixmap(-3, -1, parent->disabledDownPix); if(ordtype==KTabListBox::ComplexOrder) { a=paint->xForm(b); mbut->move(a.x()+15,a.y()+2); if(!mbut->isVisible()) mbut->show(); t+=parent->labelHeight-4; } } paint->drawText(t,(parent->labelHeight-4+fm.ascent())/2,(const char*)name());}//-----------------------------------------------------------------------------void KTabListBoxColumn::setButton(void){ if(inumber<=0) { parent->lastSelectedColumn++; inumber=parent->lastSelectedColumn; char tmp[2]; tmp[0]=48+inumber; tmp[1]=0; if(mbut) mbut->setText(tmp); parent->reorderRows(); }}//-----------------------------------------------------------------------------void KTabListBoxColumn::resetButton(void){ if(inumber>0) { if(parent->lastSelectedColumn==inumber) { parent->lastSelectedColumn--; inumber=0; if(mbut) mbut->setText(" "); if(parent->lastSelectedColumn) parent->repaint(); else // This is for simulating a NoSort (useful for special cases like // the trash folder of kmail?) parent->reorderRows(); } else { parent->adjustNumber(inumber); inumber=0; if(mbut) mbut->setText(" "); parent->reorderRows(); } }}//-----------------------------------------------------------------------------void KTabListBoxColumn::clearAll(bool leftbutton){ parent->clearAllNum(); if(leftbutton) setButton();}//=============================================================================//// C L A S S KTabListBox////=============================================================================KTabListBox::KTabListBox(QWidget *parent, const char *name, int columns, WFlags _f): KTabListBoxInherited(parent, name, _f), lbox(this){ QFontMetrics fm = fontMetrics(); QString f; QColorGroup g = colorGroup(); initMetaObject(); f = kapp->kde_datadir().copy(); f += "/khtmlw/pics/khtmlw_dnd.xpm"; dndDefaultPixmap.load(f.data()); f = kapp->kde_toolbardir().copy(); f += "/up.xpm"; upPix.load(f.data()); f = kapp->kde_toolbardir().copy(); f += "/down.xpm"; downPix.load(f.data()); QPalette pal = palette(); QColorGroup g1 = pal.disabled(); // Prepare the disabledPixmap for drawing // Maybe we can semplify it a bit, some suggestions?? disabledUpPix.resize(upPix.width(), upPix.height()); disabledDownPix.resize(downPix.width(), downPix.height()); disabledUpPix.fill( g1.background() ); disabledDownPix.fill( g1.background() ); const QBitmap *mask = upPix.mask(); const QBitmap *mask1= downPix.mask(); bool allocated = false; if (!mask) {// This shouldn't occur anymore! mask = new QBitmap(upPix.createHeuristicMask()); allocated = true; } bool allocated1 = false; if (!mask1) {// This shouldn't occur anymore! mask1 = new QBitmap(downPix.createHeuristicMask()); allocated1 = true; } QBitmap bitmap = *mask; // YES! make a DEEP copy before setting the mask! QBitmap bitmap1 = *mask1; bitmap.setMask(*mask); bitmap1.setMask(*mask1); QPainter p; p.begin( &disabledUpPix ); p.setPen( g1.light() ); p.drawPixmap(1, 1, bitmap); p.setPen( g1.mid() ); p.drawPixmap(0, 0, bitmap); p.end(); QPainter p1; p1.begin( &disabledDownPix ); p1.setPen( g1.light() ); p1.drawPixmap(1, 1, bitmap1); p1.setPen( g1.mid() ); p1.drawPixmap(0, 0, bitmap1); p1.end(); if (allocated) // This shouldn't occur anymore! delete mask; if(allocated1) delete mask1; lastSelectedColumn=0; tabPixels = 10; maxItems = 0; nMarked=0; current = -1; colList = 0L; colShowList =0L; itemList = 0L; itemShowList=0L; sepChar = '\n'; labelHeight = fm.height() + 4; columnPadding = fm.height() / 2; highlightColor = kapp->selectColor; mResizeCol = false; stopOrdering=false; needsSort=false; mSortCol = -1; numColumns = columns; mMouseCol=-1; mMouseColLeft=0; mMouseColWidth=0; mMouseAction=false; mMouseDragColumn=false; setMouseTracking(TRUE); lbox.setGeometry(0, labelHeight, width(), height()-labelHeight); if (columns > 0) setNumCols(columns);}//-----------------------------------------------------------------------------KTabListBox::~KTabListBox(){ int i; if (colList) { for (i=0; i<numColumns; i++) { delete colList[i]; colList[i] = NULL; } delete[] colList; delete[] colShowList; } if (itemList) { for (i=0; i<maxItems; i++) { delete itemList[i]; itemList[i] = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -