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

📄 q3table.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*!    This function returns whether the contents of the cell may be    replaced with the contents of another table item. Regardless of    this setting, table items that span more than one cell may not    have their contents replaced by another table item.    (This differs from \l EditType because EditType is concerned with    whether the \e user is able to change the contents of a cell.)    \sa setReplaceable() EditType*/bool Q3TableItem::isReplaceable() const{    if (rowspan > 1 || colspan > 1)	return false;    return tcha;}/*!    This virtual function returns the key that should be used for    sorting. The default implementation returns the text() of the    relevant item.    \sa Q3Table::setSorting()*/QString Q3TableItem::key() const{    return text();}/*!    This virtual function returns the size a cell needs to show its    entire content.    If you subclass Q3TableItem you will often need to reimplement this    function.*/QSize Q3TableItem::sizeHint() const{    QSize strutSize = QApplication::globalStrut();    if (edType == Always && table()->cellWidget(rw, cl))	return table()->cellWidget(rw, cl)->sizeHint().expandedTo(strutSize);    QSize s;    int x = 0;    if (!pix.isNull()) {	s = pix.size();	s.setWidth(s.width() + 2);	x = pix.width() + 2;    }    QString t = text();    if (!wordwrap && t.find('\n') == -1)	return QSize(s.width() + table()->fontMetrics().width(text()) + 10,		      QMAX(s.height(), table()->fontMetrics().height())).expandedTo(strutSize);    QRect r = table()->fontMetrics().boundingRect(x + 2, 0, table()->columnWidth(col()) - x - 4, 0,						   wordwrap ? (alignment() | WordBreak) : alignment(),						   text());    r.setWidth(QMAX(r.width() + 10, table()->columnWidth(col())));    return QSize(r.width(), QMAX(s.height(), r.height())).expandedTo(strutSize);}/*!    Changes the extent of the Q3TableItem so that it spans multiple    cells covering \a rs rows and \a cs columns. The top left cell is    the original cell.    \warning This function only works if the item has already been    inserted into the table using e.g. Q3Table::setItem(). This    function also checks to make sure if \a rs and \a cs are within    the bounds of the table and returns without changing the span if    they are not. In addition swapping, inserting or removing rows and    columns that cross Q3TableItems spanning more than one cell is not    supported.    \sa rowSpan() colSpan()*/void Q3TableItem::setSpan(int rs, int cs){    if (rs == rowspan && cs == colspan)	return;    if (!table()->d->hasRowSpan)	table()->d->hasRowSpan = rs > 1;    if (!table()->d->hasColSpan)	table()->d->hasColSpan = cs > 1;    // return if we are thinking too big...    if (rw + rs > table()->numRows())	return;    if (cl + cs > table()->numCols())	return;    if (rw == -1 || cl == -1)	return;    int rrow = rw;    int rcol = cl;    if (rowspan > 1 || colspan > 1) {	Q3Table* t = table();	t->takeItem(this);	t->setItem(rrow, rcol, this);    }    rowspan = rs;    colspan = cs;    for (int r = 0; r < rowspan; ++r) {	for (int c = 0; c < colspan; ++c) {	    if (r == 0 && c == 0)		continue;	    qt_update_cell_widget = false;	    table()->setItem(r + rw, c + cl, this);	    qt_update_cell_widget = true;	    rw = rrow;	    cl = rcol;	}    }    table()->updateCell(rw, cl);    QWidget *w = table()->cellWidget(rw, cl);    if (w)	w->resize(table()->cellGeometry(rw, cl).size());}/*!    Returns the row span of the table item, usually 1.    \sa setSpan() colSpan()*/int Q3TableItem::rowSpan() const{    return rowspan;}/*!    Returns the column span of the table item, usually 1.    \sa setSpan() rowSpan()*/int Q3TableItem::colSpan() const{    return colspan;}/*!    Sets row \a r as the table item's row. Usually you do not need to    call this function.    If the cell spans multiple rows, this function sets the top row    and retains the height of the multi-cell table item.    \sa row() setCol() rowSpan()*/void Q3TableItem::setRow(int r){    rw = r;}/*!    Sets column \a c as the table item's column. Usually you will not    need to call this function.    If the cell spans multiple columns, this function sets the    left-most column and retains the width of the multi-cell table    item.    \sa col() setRow() colSpan()*/void Q3TableItem::setCol(int c){    cl = c;}/*!    Returns the row where the table item is located. If the cell spans    multiple rows, this function returns the top-most row.    \sa col() setRow()*/int Q3TableItem::row() const{    return rw;}/*!    Returns the column where the table item is located. If the cell    spans multiple columns, this function returns the left-most    column.    \sa row() setCol()*/int Q3TableItem::col() const{    return cl;}/*!    If \a b is true, the table item is enabled; if \a b is false the    table item is disabled.    A disabled item doesn't respond to user interaction.    \sa isEnabled()*/void Q3TableItem::setEnabled(bool b){    if (b == (bool)enabled)	return;    enabled = b;    table()->updateCell(row(), col());}/*!    Returns true if the table item is enabled; otherwise returns false.    \sa setEnabled()*/bool Q3TableItem::isEnabled() const{    return (bool)enabled;}/*!    \class Q3ComboTableItem    \brief The Q3ComboTableItem class provides a means of using    comboboxes in Q3Tables.    \compat    A Q3ComboTableItem is a table item which looks and behaves like a    combobox. The advantage of using Q3ComboTableItems rather than real    comboboxes is that a Q3ComboTableItem uses far less resources than    real comboboxes in \l{Q3Table}s. When the cell has the focus it    displays a real combobox which the user can interact with. When    the cell does not have the focus the cell \e looks like a    combobox. Only text items (i.e. no pixmaps) may be used in    Q3ComboTableItems.    Q3ComboTableItem items have the edit type \c WhenCurrent (see    \l{EditType}). The Q3ComboTableItem's list of items is provided by    a QStringList passed to the constructor.    The list of items may be changed using setStringList(). The    current item can be set with setCurrentItem() and retrieved with    currentItem(). The text of the current item can be obtained with    currentText(), and the text of a particular item can be retrieved    with text().    If isEditable() is true the Q3ComboTableItem will permit the user    to either choose an existing list item, or create a new list item    by entering their own text; otherwise the user may only choose one    of the existing list items.    To populate a table cell with a Q3ComboTableItem use    Q3Table::setItem().    Q3ComboTableItems may be deleted with Q3Table::clearCell().    Q3ComboTableItems can be distinguished from \l{Q3TableItem}s and    \l{Q3CheckTableItem}s using their Run Time Type Identification    number (see rtti()).    \img qtableitems.png Table Items    \sa Q3CheckTableItem Q3TableItem QComboBox*/QComboBox *Q3ComboTableItem::fakeCombo = 0;QWidget *Q3ComboTableItem::fakeComboWidget = 0;int Q3ComboTableItem::fakeRef = 0;/*!    Creates a combo table item for the table \a table. The combobox's    list of items is passed in the \a list argument. If \a editable is    true the user may type in new list items; if \a editable is false    the user may only select from the list of items provided.    By default Q3ComboTableItems cannot be replaced by other table    items since isReplaceable() returns false by default.    \sa Q3Table::clearCell() EditType*/Q3ComboTableItem::Q3ComboTableItem(Q3Table *table, const QStringList &list, bool editable)    : Q3TableItem(table, WhenCurrent, ""), entries(list), current(0), edit(editable){    setReplaceable(false);    if (!Q3ComboTableItem::fakeCombo) {	Q3ComboTableItem::fakeComboWidget = new QWidget(0, 0);	Q3ComboTableItem::fakeCombo = new QComboBox(false, Q3ComboTableItem::fakeComboWidget, 0);	Q3ComboTableItem::fakeCombo->hide();    }    ++Q3ComboTableItem::fakeRef;    if (entries.count())	setText(entries.at(current));}/*!    Q3ComboTableItem destructor.*/Q3ComboTableItem::~Q3ComboTableItem(){    if (--Q3ComboTableItem::fakeRef <= 0) {	delete Q3ComboTableItem::fakeComboWidget;	Q3ComboTableItem::fakeComboWidget = 0;	Q3ComboTableItem::fakeCombo = 0;    }}/*!    Sets the list items of this Q3ComboTableItem to the strings in the    string list \a l.*/void Q3ComboTableItem::setStringList(const QStringList &l){    entries = l;    current = 0;    if (entries.count())	setText(entries.at(current));    if (table()->cellWidget(row(), col())) {	cb->clear();	cb->insertStringList(entries);    }    table()->updateCell(row(), col());}/*! \reimp */QWidget *Q3ComboTableItem::createEditor() const{    // create an editor - a combobox in our case    ((Q3ComboTableItem*)this)->cb = new QComboBox(edit, table()->viewport(), "qt_editor_cb");    cb->insertStringList(entries);    cb->setCurrentItem(current);    QObject::connect(cb, SIGNAL(activated(int)), table(), SLOT(doValueChanged()));    return cb;}/*! \reimp */void Q3ComboTableItem::setContentFromEditor(QWidget *w){    QComboBox *cb = ::qobject_cast<QComboBox*>(w);    if (cb) {	entries.clear();	for (int i = 0; i < cb->count(); ++i)	    entries << cb->text(i);	current = cb->currentItem();	setText(entries.at(current));    }}/*! \reimp */void Q3ComboTableItem::paint(QPainter *p, const QColorGroup &cg,			   const QRect &cr, bool selected){    fakeCombo->resize(cr.width(), cr.height());    QPalette pal2(cg);    if (selected) {        pal2.setBrush(QPalette::Base, cg.QPalette::brush(QPalette::Highlight));        pal2.setColor(QPalette::Text, cg.highlightedText());    }    QStyle::State flags = QStyle::State_None;    if(isEnabled() && table()->isEnabled())        flags |= QStyle::State_Enabled;    // Since we still have the "fakeCombo" may as well use it in this case.    QStyleOptionComboBox opt;    opt.initFrom(table());    opt.rect = fakeCombo->rect();    opt.palette = pal2;    opt.state &= ~QStyle::State_HasFocus;    opt.state &= ~QStyle::State_MouseOver;    opt.state |= flags;    opt.subControls = QStyle::SC_All;    opt.activeSubControls = QStyle::SC_None;    opt.editable = fakeCombo->editable();    table()->style()->drawComplexControl(QStyle::CC_ComboBox, &opt, p, fakeCombo);    p->save();    QRect textR = table()->style()->subControlRect(QStyle::CC_ComboBox, &opt,                                                   QStyle::SC_ComboBoxEditField, fakeCombo);    int align = alignment(); // alignment() changes entries    p->drawText(textR, wordWrap() ? (align | Qt::WordBreak) : align, entries.at(current));    p->restore();}/*!    Sets the list item \a i to be the combo table item's current list    item.    \sa currentItem()*/void Q3ComboTableItem::setCurrentItem(int i){    QWidget *w = table()->cellWidget(row(), col());    QComboBox *cb = ::qobject_cast<QComboBox*>(w);    if (cb) {	cb->setCurrentItem(i);	current = i;	setText(cb->currentText());    } else {	current = i;	setText(entries.at(i));	table()->updateCell(row(), col());    }}/*!    \overload    Sets the list item whose text is \a s to be the combo table item's    current list item. Does nothing if no list item has the text \a s.    \sa currentItem()*/void Q3ComboTableItem::setCurrentItem(const QString &s){    int i = entries.findIndex(s);    if (i != -1)	setCurrentItem(i);}/*!    Returns the index of the combo table item's current list item.

⌨️ 快捷键说明

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