⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctableview.cpp

📁 Gambas is a graphical development environment based on a Basic interpreter, like Visual Basic. It us
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************  CTableView.cpp  The TableView control  (c) 2000-2003 Beno� Minisini <gambas@users.sourceforge.net>  This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 1, or (at your option)  any later version.  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS F A PARTICULAR PURPOSE.  See the  GNU General Public License for more details.  You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.***************************************************************************/#define __CTABLEVIEW_CPP#include "main.h"#include <qapplication.h>#include <qheader.h>#include <qpainter.h>#include <qpalette.h>#include "CTableView.h"DECLARE_EVENT(EVENT_Click);DECLARE_EVENT(EVENT_Activate);DECLARE_EVENT(EVENT_Scroll);DECLARE_EVENT(EVENT_Data);DECLARE_EVENT(EVENT_ColumnClick);DECLARE_EVENT(EVENT_RowClick);DECLARE_EVENT(EVENT_Change);/***************************************************************************  class MyTableItem***************************************************************************/MyTableItem::MyTableItem(QTable *table): QTableItem(table, QTableItem::Never, 0){  _tableView = 0;  _bg = -1;  _fg = -1;}MyTableItem::~MyTableItem(){}void MyTableItem::setPicture(GB_OBJECT *val){  QT_PICTURE pict;  pict = (QT_PICTURE)VALUE(val);  if (pict)    setPixmap(*QT.GetPixmap(pict));  else    setPixmap(0);}void MyTableItem::invalidate(){  _valid = false;  _alignment = Qt::AlignLeft | Qt::AlignVCenter;  _bg = -1;  _fg = -1;  setText(0);  setPixmap(0);}bool MyTableItem::invalidate(int r, int c){  if (r == row() && c == col())    return true;  setRow(r);  setCol(c);    invalidate();  return false;}void MyTableItem::getData(){  if (_valid)    return;  //qDebug("Data(%d, %d)", row(), col());  if (!_tableView)    _tableView = (CTABLEVIEW *)QT.GetObject(table());  if (!_tableView)    return;  _valid = true;  GB.Raise(_tableView, EVENT_Data, 2,    GB_T_INTEGER, row(),    GB_T_INTEGER, col()    );}QString MyTableItem::text(){  getData();  return QTableItem::text();}QPixmap MyTableItem::pixmap(){  getData();  return QTableItem::pixmap();}int MyTableItem::alignment() const{  ((MyTableItem *)this)->getData();  return _alignment;}void MyTableItem::paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected ){  int w = cr.width();  int h = cr.height();  int x = 0;  getData();  QPixmap pix = pixmap();  QString txt = text();  p->fillRect( 0, 0, w, h,    selected ?      cg.brush(QColorGroup::Highlight)      : (_bg < 0) ? cg.brush(QColorGroup::Base) : QColor((uint)_bg));  if (!pix.isNull())  {    if (txt.length() == 0)      p->drawPixmap((w - pix.width()) / 2, (h - pix.height() ) / 2, pix);    else    {      p->drawPixmap(2, (h - pix.height() ) / 2, pix);      x = pix.width() + 4;    }  }  if (selected)    p->setPen(cg.highlightedText());  else if (_fg < 0)    p->setPen(cg.text());  else    p->setPen(QColor((uint)_fg));  p->drawText(x + 2, 0, w - x - 4, h,    wordWrap() ? (_alignment | WordBreak) : _alignment, txt );}/***************************************************************************  class MyTable***************************************************************************/MyTable::MyTable(QWidget *parent) :QTable(0, 0, parent){  _item = new MyTableItem(this);  //_items = (MyTableItem **)0;  _header = 3;  _rows = 0;  _cols = 0;  //setReadOnly(true);  setSelectionMode(NoSelection);  setFocusStyle(FollowStyle);  verticalHeader()->setMovingEnabled(false);  horizontalHeader()->setMovingEnabled(false);  updateHeaders();}MyTable::~MyTable(){  //qWarning("~MyTable");  //blockSignals(true);  //setUpdatesEnabled(false);  //setNumCols(0);  //setNumRows(0);  delete _item;  //qWarning("~MyTable: end");  //blockSignals(false);}void MyTable::paintFocus( QPainter *p, const QRect &r ){}/*QSize MyTable::tableSize() const{  return QSize( columnPos( numCols() - 1 ) + columnWidth( numCols() - 1 ),    rowPos( numRows() - 1 ) + rowHeight( numRows() - 1 ) );}*/void MyTable::setRowHeight(int row, long height){  //qDebug("MyTable::setRowHeight(%d, %ld)", row, height);  if (height < 0)    adjustRow(row);  else    QTable::setRowHeight(row, height);}void MyTable::setColumnWidth(int col, long width){  //qDebug("MyTable::setColumnWidth(%d, %ld)", col, width);  if (width < 0)    adjustColumn(col);  else    QTable::setColumnWidth(col, width);}void MyTable::updateHeaders(){  int dim = fontMetrics().height() + 4;  if (_header & 1)  {    horizontalHeader()->show();    setTopMargin(dim);  }  else  {    horizontalHeader()->hide();    setTopMargin(0);  }  if (leftMargin() > dim)    dim = leftMargin();    if (_header & 2)  {    verticalHeader()->show();    setLeftMargin(dim);  }  else  {    verticalHeader()->hide();    setLeftMargin(0);  }}void MyTable::setHeaders(int h){  h &= 3;  if (h == _header)    return;  _header = h;  updateHeaders();}void MyTable::fontChange(const QFont &oldFont){  QTable::fontChange(oldFont);  updateHeaders();}QTableItem *MyTable::item( int row, int col ) const{  if ( row < 0 || col < 0 || row > _rows - 1 || col > _cols - 1)    return 0;  //_items[col]->invalidate(row, col);  //return _items[col];  _item->invalidate(row, col);  return _item;}void MyTable::setNumCols(int newCols){  int i;  int col = numCols();  if (newCols < 0)    return;  _cols = newCols;  _item->invalidate();  /*  if (_items)  {    delete[] _items;    _items = (MyTableItem **)0;  }  if (_cols > 0)  {    _items = new (MyTableItem *)[_cols];    for (i = 0; i < _cols; i++)      _items[i] = new MyTableItem(this);  }  */  QTable::setNumCols(newCols);  if (newCols > col)  {    bool upd = horizontalHeader()->isUpdatesEnabled();    horizontalHeader()->setUpdatesEnabled(false);    for (i = col; i < newCols; i++)      horizontalHeader()->setLabel(i, "");    horizontalHeader()->setUpdatesEnabled(upd);  }}void MyTable::setNumRows(int newRows){  //int i;  //int row = numRows();  //bool r = verticalHeader()->isResizeEnabled();  //setLeftMargin(fontMetrics().width(QString::number(newRows) + "W"));  //verticalHeader()->setResizeEnabled(false);  if (newRows < 0)    return;  _rows = newRows;  _item->invalidate();  QTable::setNumRows(newRows);  //verticalHeader()->setResizeEnabled(r);  /*if (newRows > row)  {    bool upd = verticalHeader()->isUpdatesEnabled();    verticalHeader()->setUpdatesEnabled(false);    for (i = row; i < newRows; i++)      verticalHeader()->setLabel(i, QString::number(i + 1));    verticalHeader()->setUpdatesEnabled(upd);  }*/}void MyTable::updateRow(int row){  if (row < 0 || row >= numRows() || numCols() == 0)    return;  QRect cg = cellGeometry(row, 0);  QRect r(contentsToViewport( QPoint( contentsX(), cg.y() - 2 ) ),    QSize( contentsWidth(), cg.height() + 4 ) );    QApplication::postEvent(viewport(), new QPaintEvent(r, FALSE ));}/*void MyTable::swapRows(int row1, int row2, bool swapHeader){  QTable::swapRows(row1, row2, swapHeader);  updateRow(row1);  updateRow(row2);}*/void MyTable::updateColumn(int col){  if (col < 0 || col >= numCols() || numRows() == 0)    return;  QRect cg = cellGeometry(0, col);  QRect r(contentsToViewport( QPoint( cg.x() - 2, contentsY() ) ),    QSize( cg.width() + 4, contentsHeight() ) );  QApplication::postEvent( viewport(), new QPaintEvent( r, FALSE ) );}/*void MyTable::swapColumns(int col1, int col2, bool swapHeader){  QTable::swapColumns(col1, col2, swapHeader);  updateColumn(col1);  updateColumn(col2);}*//***************************************************************************  TableView***************************************************************************/BEGIN_METHOD(CTABLEVIEW_new, GB_OBJECT parent)  MyTable *wid = new MyTable(QT.GetContainer(VARG(parent)));  QObject::connect(wid, SIGNAL(currentChanged(int, int)), MANAGER, SLOT(changed()));  QObject::connect(wid, SIGNAL(doubleClicked(int, int, int, const QPoint &)), MANAGER, SLOT(activated()));  QObject::connect(wid, SIGNAL(clicked(int, int, int, const QPoint &)), MANAGER, SLOT(clicked()));  QObject::connect(wid, SIGNAL(contentsMoving(int, int)), MANAGER, SLOT(scrolled()));  QObject::connect(wid->horizontalHeader(), SIGNAL(sectionClicked(int)), MANAGER, SLOT(columnClicked(int)));  QObject::connect(wid->verticalHeader(), SIGNAL(sectionClicked(int)), MANAGER, SLOT(rowClicked(int)));  QT.InitWidget(wid, _object);  //QT.SetBackgroundRole(THIS, QColorGroup::Base);  THIS->row = -1;  THIS->col = -1;  //wid->setTableView(THIS);  wid->show();END_METHODBEGIN_METHOD_VOID(CTABLEVIEW_free)  GB.StoreObject(NULL, &THIS->picture);END_METHODBEGIN_PROPERTY(CTABLEVIEW_row)  if (READ_PROPERTY)    GB.ReturnInteger(WIDGET->currentRow());  else    WIDGET->setCurrentCell(VPROP(GB_INTEGER), WIDGET->currentColumn());END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_column)  if (READ_PROPERTY)    GB.ReturnInteger(WIDGET->currentColumn());  else    WIDGET->setCurrentCell(WIDGET->currentRow(), VPROP(GB_INTEGER));END_PROPERTYBEGIN_METHOD(CTABLEVIEW_move_to, GB_INTEGER row; GB_INTEGER col)  long row = VARG(row);  long col = VARG(col);  if (CTableView::check(WIDGET, row, col))    return;  WIDGET->setCurrentCell(row, col);END_METHOD#if 0BEGIN_METHOD_VOID(CTABLEVIEW_clear)  long rows = WIDGET->numRows();  WIDGET->setNumRows(0);  WIDGET->setNumRows(rows);END_METHOD#endifBEGIN_METHOD(CTABLEVIEW_get, GB_INTEGER row; GB_INTEGER col)  long row = VARG(row);  long col = VARG(col);  if (CTableView::check(WIDGET, row, col))    return;  THIS->row = row;  THIS->col = col;  RETURN_SELF();END_METHODBEGIN_PROPERTY(CTABLEVIEW_current)  THIS->row = WIDGET->currentRow();  THIS->col = WIDGET->currentColumn();    if (CTableView::check(WIDGET, THIS->row, THIS->col))    return;  else      RETURN_SELF();END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_self)  THIS->row = -1;  THIS->col = -1;  RETURN_SELF();END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_grid)  if (READ_PROPERTY)    GB.ReturnBoolean(WIDGET->showGrid());  else    WIDGET->setShowGrid(VPROP(GB_BOOLEAN));END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_scrollbars)  long scroll;  if (READ_PROPERTY)  {    scroll = 0;    if (WIDGET->hScrollBarMode() == QScrollView::Auto)      scroll += 1;    if (WIDGET->vScrollBarMode() == QScrollView::Auto)      scroll += 2;    GB.ReturnInteger(scroll);  }  else  {    scroll = VPROP(GB_INTEGER) & 3;    WIDGET->setHScrollBarMode( (scroll & 1) ? QScrollView::Auto : QScrollView::AlwaysOff);    WIDGET->setVScrollBarMode( (scroll & 2) ? QScrollView::Auto : QScrollView::AlwaysOff);  }END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_header)  if (READ_PROPERTY)    GB.ReturnInteger(WIDGET->headers());  else    WIDGET->setHeaders(VPROP(GB_INTEGER));END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_mode)  if (READ_PROPERTY)  {    switch (WIDGET->selectionMode())    {      case QTable::NoSelection: GB.ReturnInteger(0); break;      case QTable::SingleRow: GB.ReturnInteger(1); break;      case QTable::MultiRow: GB.ReturnInteger(2); break;      default: GB.ReturnInteger(0); break;    }  }  else  {    switch(VPROP(GB_INTEGER))    {      case 0: WIDGET->setSelectionMode(QTable::NoSelection); break;      case 1: WIDGET->setSelectionMode(QTable::SingleRow); break;      case 2: WIDGET->setSelectionMode(QTable::MultiRow); break;    }  }END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_client_width)  WIDGET->updateScrollBars();  GB.ReturnInteger(WIDGET->clipper()->width());END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_client_height)  WIDGET->updateScrollBars();  GB.ReturnInteger(WIDGET->clipper()->height());END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_border)  QT.BorderProperty(_object, _param);END_PROPERTY// CWIDGET_refresh does not work for this widget ??// Should do viewport->refresh() !BEGIN_METHOD(CTABLEVIEW_refresh, GB_INTEGER x; GB_INTEGER y; GB_INTEGER w; GB_INTEGER h)  int x, y, w, h;  if (!MISSING(x) && !MISSING(y))  {    x = VARG(x);    y = VARG(y);    w = VARGOPT(w, WIDGET->width());    h = VARGOPT(h, WIDGET->height());    WIDGET->viewport()->repaint(x, y, w, h);  }  else    WIDGET->viewport()->repaint();END_METHODBEGIN_PROPERTY(CTABLEVIEW_scroll_x)  if (READ_PROPERTY)    GB.ReturnInteger(WIDGET->contentsX());  else    WIDGET->setContentsPos(VPROP(GB_INTEGER), WIDGET->contentsY());END_PROPERTYBEGIN_PROPERTY(CTABLEVIEW_scroll_y)  if (READ_PROPERTY)    GB.ReturnInteger(WIDGET->contentsY());  else    WIDGET->setContentsPos(WIDGET->contentsX(), VPROP(GB_INTEGER));END_PROPERTYBEGIN_METHOD(CTABLEVIEW_row_at, GB_INTEGER ypos)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -