void QtlCheckableHeaderView::mousePressEvent(QMouseEvent* event) { // Process this event when it occurs in the first section of the header, inside the checkbox. if (event != 0 && logicalIndexAt(event->pos()) == 0 && checkBoxRect().contains(event->pos())) { // We force either checked or unchecked in repaint. if (_checkState != Qt::Unchecked) { _checkState = Qt::Unchecked; } else { _checkState = Qt::Checked; } _forceCheck = true; // The state always changes here, process the change. processCheckStateChanged(); // Update all check boxes in first column. updateAllRows(); } // Update the widget. update(); // Let the superclass process other effects for this event. QHeaderView::mousePressEvent(event); }
TasksViewDelegate::Layout TasksViewDelegate::doLayout(const QStyleOptionViewItem &option, const QModelIndex &index) const { Layout layout; // Find size of checkbox const QVariant checkStateVariant = index.data(Qt::CheckStateRole); layout.cbRect = checkBoxRect(option, checkStateVariant); layout.height = qMax(layout.cbRect.height(), option.fontMetrics.height()); return layout; }
void TasksViewDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { // TODO use doLayout const QRect cbRect = checkBoxRect(option, Qt::Checked); int firstLineHeight = qMax(cbRect.height(), option.fontMetrics.height()); const QVariant decorationVariant = index.data(Qt::DecorationRole); const QPixmap decorationPixmap = decoration(option, decorationVariant); const QString runningTime = index.data(TasksViewRole_RunningTime).toString(); const int left = decorationPixmap.width() + option.fontMetrics.width(runningTime); QRect r = option.rect.translated(left + 5, 0); r.setRight(cbRect.left()); r.setTop(r.top() + firstLineHeight); editor->setGeometry(r); }
QRect lmcUserTreeWidgetItem::checkBoxRect(const QRect& itemRect) { QRect checkBoxRect(0, 0, 0, 0); if(data(0, TypeRole).toString() == "Group") checkBoxRect = QRect(itemRect.left(), itemRect.top() + ((itemRect.height() - 12) / 2), 0, 0); else if(data(0, TypeRole).toString() == "User") checkBoxRect = QRect(itemRect.left(), itemRect.top() + 4, 0, 0); lmcUserTreeWidget* treeWidget = (lmcUserTreeWidget*)this->treeWidget(); if(treeWidget->checkable()) { checkBoxRect.setSize(QSize(12, 12)); checkBoxRect.moveLeft(checkBoxRect.left() + 3); } return checkBoxRect; }
bool TasksViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { Q_ASSERT(event); Q_ASSERT(model); // make sure that the item is checkable Qt::ItemFlags flags = model->flags(index); if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled) || !(flags & Qt::ItemIsEnabled)) return false; // make sure that we have a check state QVariant value = index.data(Qt::CheckStateRole); if (!value.isValid()) return false; // make sure that we have the right event type if ((event->type() == QEvent::MouseButtonRelease) || (event->type() == QEvent::MouseButtonDblClick)) { const QRect checkRect = checkBoxRect(option, Qt::Checked); QMouseEvent *me = static_cast<QMouseEvent*>(event); if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos())) return false; // eat the double click events inside the check rect if (event->type() == QEvent::MouseButtonDblClick) return true; } else if (event->type() == QEvent::KeyPress) { if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select) return false; } else { return false; } Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); return model->setData(index, state, Qt::CheckStateRole); }
void QtlCheckableHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const { // Invoke the superclass to do the main job. painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); // We need to paint the checkbox when we are in section 0 only. // We also need a model (sanity check). QAbstractItemModel* model = this->model(); if (model == 0 || logicalIndex != 0) { return; } // Count the total number of checkable and checked items in the first column. int checkableCount = 0; int checkedCount = 0; for (int row = 0; row < model->rowCount(); row++) { const QModelIndex index = model->index(row, 0); if (index.isValid() && (model->flags(index) & Qt::ItemIsUserCheckable) != 0) { checkableCount++; const Qt::CheckState state= model->data(index, Qt::CheckStateRole).value<Qt::CheckState>(); if (state != Qt::Unchecked) { checkedCount++; } } } // Final position and size of the checkbox. QStyleOptionButton option; option.rect = checkBoxRect(rect.topLeft()); option.state |= QStyle::State_Enabled; // Compute the new check state. Qt::CheckState newCheckState = _checkState; if (_forceCheck) { // Force either checked or unchecked. Do it once only. _forceCheck = false; if (_checkState == Qt::Unchecked) { option.state |= QStyle::State_Off; newCheckState = Qt::Unchecked; } else { option.state |= QStyle::State_On; newCheckState = Qt::Checked; } } else if (checkedCount == 0) { option.state |= QStyle::State_Off; newCheckState = Qt::Unchecked; } else if (checkedCount < checkableCount) { option.state |= QStyle::State_NoChange; newCheckState = Qt::PartiallyChecked; } else if (checkedCount == checkableCount) { option.state |= QStyle::State_On; newCheckState = Qt::Checked; } else { // Should not get there. Q_ASSERT(false); option.state |= QStyle::State_None; } // Now draw the checkbox. style()->drawControl(QStyle::CE_CheckBox, &option, painter); // Finally report change in state. if (_checkState != newCheckState) { _checkState = newCheckState; const_cast<QtlCheckableHeaderView*>(this)->processCheckStateChanged(); } }
AutomountSettingsPanel::AutomountSettingsPanel(BRect frame, BMessage *settings, AutoMounter *target) : BBox(frame, "", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, B_PLAIN_BORDER), fTarget(target) { SetViewColor(kLightGray); BRect checkBoxRect(Bounds()); BRect boxRect(Bounds()); boxRect.InsetBy(10, 15); boxRect.bottom = boxRect.top + 85; BBox *box = new BBox(boxRect, "autoMountBox", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS | B_PULSE_NEEDED | B_NAVIGABLE_JUMP); box->SetLabel("Automatic Disk Mounting:"); AddChild(box); checkBoxRect = box->Bounds(); checkBoxRect.InsetBy(10, 18); checkBoxRect.bottom = checkBoxRect.top + 20; scanningDisabledCheck = new BRadioButton(checkBoxRect, "scanningOff", "Don't Automount", new BMessage(kAutomountSettingsChanged)); box->AddChild(scanningDisabledCheck); checkBoxRect.OffsetBy(0, kCheckBoxSpacing); autoMountAllBFSCheck = new BRadioButton(checkBoxRect, "autoBFS", "All BeOS Disks", new BMessage(kAutomountSettingsChanged)); box->AddChild(autoMountAllBFSCheck); checkBoxRect.OffsetBy(0, kCheckBoxSpacing); autoMountAllCheck = new BRadioButton(checkBoxRect, "autoAll", "All Disks", new BMessage(kAutomountSettingsChanged)); box->AddChild(autoMountAllCheck); boxRect.OffsetTo(boxRect.left, boxRect.bottom + 15); boxRect.bottom = boxRect.top + 105; box = new BBox(boxRect, "", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS | B_PULSE_NEEDED | B_NAVIGABLE_JUMP); box->SetLabel("Disk Mounting During Boot:"); AddChild(box); checkBoxRect = box->Bounds(); checkBoxRect.InsetBy(10, 18); checkBoxRect.bottom = checkBoxRect.top + 20; initialDontMountCheck = new BRadioButton(checkBoxRect, "initialNone", "Only The Boot Disk", new BMessage(kBootMountSettingsChanged)); box->AddChild(initialDontMountCheck); checkBoxRect.OffsetBy(0, kCheckBoxSpacing); initialMountRestoreCheck = new BRadioButton(checkBoxRect, "initialRestore", "Previously Mounted Disks", new BMessage(kBootMountSettingsChanged)); box->AddChild(initialMountRestoreCheck); checkBoxRect.OffsetBy(0, kCheckBoxSpacing); initialMountAllBFSCheck = new BRadioButton(checkBoxRect, "initialBFS", "All BeOS Disks", new BMessage(kBootMountSettingsChanged)); box->AddChild(initialMountAllBFSCheck); checkBoxRect.OffsetBy(0, kCheckBoxSpacing); initialMountAllCheck = new BRadioButton(checkBoxRect, "initialAll", "All Disks", new BMessage(kBootMountSettingsChanged)); box->AddChild(initialMountAllCheck); BRect buttonRect(Bounds()); buttonRect.InsetBy(15, 15); buttonRect.SetLeftTop(buttonRect.RightBottom() - kSmallButtonSize); fDone = new BButton(buttonRect, "done", "Done", new BMessage(kDone)); buttonRect.OffsetTo(buttonRect.left - 15 - buttonRect.Width(), buttonRect.top); buttonRect.left = buttonRect.left - 60; fMountAllNow = new BButton(buttonRect, "mountAll", "Mount all disks now", new BMessage(kMountAllNow)); AddChild(fMountAllNow); AddChild(fDone); fDone->MakeDefault(true); bool result; if (settings->FindBool("autoMountAll", &result) == B_OK && result) autoMountAllCheck->SetValue(1); else if (settings->FindBool("autoMountAllBFS", &result) == B_OK && result) autoMountAllBFSCheck->SetValue(1); else scanningDisabledCheck->SetValue(1); if (settings->FindBool("suspended", &result) == B_OK && result) scanningDisabledCheck->SetValue(1); if (settings->FindBool("initialMountAll", &result) == B_OK && result) initialMountAllCheck->SetValue(1); else if (settings->FindBool("initialMountRestore", &result) == B_OK && result) initialMountRestoreCheck->SetValue(1); else if (settings->FindBool("initialMountAllBFS", &result) == B_OK && result) initialMountAllBFSCheck->SetValue(1); else initialDontMountCheck->SetValue(1); }