bool HistoryView::update() { int stRow = row(st->sha()); if (stRow == -1) return false; // main/tree view asked us a sha not in history QModelIndex index = currentIndex(); QItemSelectionModel* sel = selectionModel(); if (index.isValid() && (index.row() == stRow)) { if (sel->isSelected(index) != st->selectItem()) sel->select(index, QItemSelectionModel::Toggle); scrollTo(index); } else { // setCurrentIndex() does not clear previous // selections in a multi selection QListView clearSelection(); QModelIndex newIndex = model()->index(stRow, 0); if (newIndex.isValid()) { // emits QItemSelectionModel::currentChanged() setCurrentIndex(newIndex); scrollTo(newIndex); if (!st->selectItem()) sel->select(newIndex, QItemSelectionModel::Deselect); } } if (git->isMainHistory(fh)) emit diffTargetChanged(row(st->diffToSha())); return currentIndex().isValid(); }
bool ContentWindow::eventFilter(QObject *o, QEvent *e) { if (m_contentWidget && o == m_contentWidget->viewport() && e->type() == QEvent::MouseButtonRelease) { QMouseEvent *me = static_cast<QMouseEvent*>(e); QItemSelectionModel *sm = m_contentWidget->selectionModel(); if (!me || !sm) return QWidget::eventFilter(o, e); Qt::MouseButtons button = me->button(); const QModelIndex &index = m_contentWidget->indexAt(me->pos()); if (index.isValid() && sm->isSelected(index)) { if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier)) || (button == Qt::MidButton)) { QHelpContentModel *contentModel = qobject_cast<QHelpContentModel*>(m_contentWidget->model()); if (contentModel) { QHelpContentItem *itm = contentModel->contentItemAt(index); if (itm && HelpViewer::canOpenPage(itm->url().path())) OpenPagesManager::instance().createPage(itm->url()); } } else if (button == Qt::LeftButton) { itemClicked(index); } } } return QWidget::eventFilter(o, e); }
bool ContentWindow::eventFilter(QObject *o, QEvent *e) { if (m_contentWidget && o == m_contentWidget->viewport() && e->type() == QEvent::MouseButtonRelease) { QMouseEvent *me = static_cast<QMouseEvent*>(e); QModelIndex index = m_contentWidget->indexAt(me->pos()); QItemSelectionModel *sm = m_contentWidget->selectionModel(); Qt::MouseButtons button = me->button(); if (index.isValid() && (sm && sm->isSelected(index))) { if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier)) || (button == Qt::MidButton)) { QHelpContentModel *contentModel = qobject_cast<QHelpContentModel*>(m_contentWidget->model()); if (contentModel) { QHelpContentItem *itm = contentModel->contentItemAt(index); if (itm && !isPdfFile(itm)) CentralWidget::instance()->setSourceInNewTab(itm->url()); } } else if (button == Qt::LeftButton) { itemClicked(index); } } } return QWidget::eventFilter(o, e); }
void MList::selectItem(const QModelIndex &index) { QItemSelectionModel *sModel = selectionModel(); if (index.isValid() && sModel->model() != index.model()) { qWarning("MList::selectItem() failed: " "Trying to select an item that is for" " a different model than the view "); return; } if (sModel != NULL) { if (selectionMode() == MList::MultiSelection) { if (sModel->isSelected(index)) { sModel->select(index, QItemSelectionModel::Deselect); } else { sModel->select(index, QItemSelectionModel::Select); } } else if (selectionMode() == MList::SingleSelection) { sModel->select(index, QItemSelectionModel::ClearAndSelect); } } emit itemClicked(index); }
// this slot handles auto-selection of items. void FolderView::onAutoSelectionTimeout() { if(QApplication::mouseButtons() != Qt::NoButton) return; Qt::KeyboardModifiers mods = QApplication::keyboardModifiers(); QPoint pos = view->viewport()->mapFromGlobal(QCursor::pos()); // convert to viewport coordinates QModelIndex index = view->indexAt(pos); // find out the hovered item QItemSelectionModel::SelectionFlags flags = (mode == DetailedListMode ? QItemSelectionModel::Rows : QItemSelectionModel::NoUpdate); QItemSelectionModel* selModel = view->selectionModel(); if(mods & Qt::ControlModifier) { // Ctrl key is pressed if(selModel->isSelected(index) && index != lastAutoSelectionIndex_) { // unselect a previously selected item selModel->select(index, flags|QItemSelectionModel::Deselect); lastAutoSelectionIndex_ = QModelIndex(); } else { // select an unselected item selModel->select(index, flags|QItemSelectionModel::Select); lastAutoSelectionIndex_ = index; } selModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); // move the cursor } else if(mods & Qt::ShiftModifier) { // Shift key is pressed // select all items between current index and the hovered index. QModelIndex current = selModel->currentIndex(); if(selModel->hasSelection() && current.isValid()) { selModel->clear(); // clear old selection selModel->setCurrentIndex(current, QItemSelectionModel::NoUpdate); int begin = current.row(); int end = index.row(); if(begin > end) qSwap(begin, end); for(int row = begin; row <= end; ++row) { QModelIndex sel = model_->index(row, 0); selModel->select(sel, flags|QItemSelectionModel::Select); } } else { // no items are selected, select the hovered item. if(index.isValid()) { selModel->select(index, flags|QItemSelectionModel::SelectCurrent); selModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); } } lastAutoSelectionIndex_ = index; } else if(mods == Qt::NoModifier) { // no modifier keys are pressed. if(index.isValid()) { // select the hovered item view->clearSelection(); selModel->select(index, flags|QItemSelectionModel::SelectCurrent); selModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); } lastAutoSelectionIndex_ = index; } autoSelectionTimer_->deleteLater(); autoSelectionTimer_ = nullptr; }
void ccDBRoot::selectEntity(ccHObject* obj) { bool ctrlPushed = (QApplication::keyboardModifiers () & Qt::ControlModifier); QItemSelectionModel* selectionModel = m_dbTreeWidget->selectionModel(); assert(selectionModel); //valid object? then we will try to select (or toggle) it if (obj) { QModelIndex selectedIndex = index(obj); if (selectedIndex.isValid()) { //if CTRL is pushed if (ctrlPushed) { //default case: toggle current item selection state if (!obj->isSelected()) { QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); if (!selectedIndexes.empty()) { //special case: labels can only be merged with labels! if (obj->isA(CC_2D_LABEL) != static_cast<ccHObject*>(selectedIndexes[0].internalPointer())->isA(CC_2D_LABEL)) { ccLog::Warning("[Selection] Labels and other entities can't be mixed! (release the CTRL key to start a new selection)"); return; } } } selectionModel->select(selectedIndex,QItemSelectionModel::Toggle); } else { if (selectionModel->isSelected(selectedIndex)) //nothing to do return; selectionModel->select(selectedIndex,QItemSelectionModel::ClearAndSelect); } //hack: auto-scroll to selected element if (obj->isSelected() && !ctrlPushed) m_dbTreeWidget->scrollTo(selectedIndex); } } //otherwise we clear current selection (if CTRL is not pushed) else if (!ctrlPushed) { selectionModel->clear(); } }
void StarEditor::paintEvent(QPaintEvent*) { // If a StarEditor is open, by definition the mouse is hovering over us. m_styleOption.state |= QStyle::State_MouseOver; m_styleOption.rect = rect(); if (m_pTableView) { QItemSelectionModel* selectionModel = m_pTableView->selectionModel(); if (selectionModel && selectionModel->isSelected(m_index)) { m_styleOption.state |= QStyle::State_Selected; } } QPainter painter(this); renderHelper(&painter, m_pTableView, m_styleOption, &m_starRating); }
void LoaderStoryPage::on_story_selected(QModelIndex _idx){ QItemSelectionModel * selection = this->storytree->selectionModel(); //StoryNode * current_storyNode; if(selection->isSelected(_idx)){ StoryItem * tmp =static_cast<StoryItem*>(this->my_treestory->itemFromIndex(_idx)); if(tmp->isRoot()){ QMessageBox::information(this,"added element","can't add a term to experiment"); } else{ this->current_storyNode=this->currentDoc->get_storyboard()->findNode(tmp->get_story()->get_label()); this->storyChosen->setText(tmp->get_story()->get_label()); std::cerr << this->current_storyNode->get_label().toStdString() << std::endl; } } }
QString LocalView::get_selected_directory() { qDebug() <<__FUNCTION__<<": "<<__LINE__<<":"<< __FILE__; QString local_path; QItemSelectionModel *ism = this->uiw->treeView->selectionModel(); QModelIndex cidx, idx; if (ism == 0) { return QString(); } if (!ism->hasSelection()) { qDebug()<<"why no tree selection???"; return QString(); } // QModelIndexList mil = ism->selectedIndexes(); // if (mil.count() == 0) { // return QString(); // } //qDebug() << mil ; //qDebug() << model->fileName ( mil.at ( 0 ) ); //qDebug() << model->filePath ( mil.at ( 0 ) ); cidx = ism->currentIndex(); if (!ism->isSelected(cidx)) { // so currentIndex is not always a selected index !!!!!! qDebug()<<"Why current index is not a selected index???"; } idx = ism->model()->index(cidx.row(), 0, cidx.parent()); // QString local_file = this->dir_file_model->filePath(mil.at(0)); // local_path = this->dir_file_model->filePath(mil.at(0)); QString local_file = this->dir_file_model->filePath(idx); local_path = this->dir_file_model->filePath(idx); return local_path; }
void ListView_Impl::paintDropTarget(QPainter *painter, const QModelIndex& index, int where) { QStyleOptionViewItem option = viewOptions(); QRect rect = this->visualRect(index); QWidget *viewport = this->viewport(); QColor highlight = palette().color(QPalette::HighlightedText); QColor color = option.state & QStyle::State_Selected ? highlight : palette().color(QPalette::Highlight); QPen pen(color); painter->save(); if (!index.isValid()) where = SL_EVENT_DRAG_ON_VIEWPORT; switch (where) { case SL_EVENT_DRAG_BELOW_ITEM: case SL_EVENT_DRAG_ABOVE_ITEM: { if (viewMode() == IconMode) { QSize size = gridSize(); if (size.isEmpty()) size = rect.size(); int x, y, height = size.height(); int cellWidth = size.width() + spacing(); int cellHeight = height + spacing(); x = rect.left() + horizontalOffset(); if (where == SL_EVENT_DRAG_BELOW_ITEM) x += cellWidth; x = ((x / cellWidth) * cellWidth) - horizontalOffset(); y = (((rect.top() + verticalOffset()) / cellHeight) * cellHeight) - verticalOffset(); height = qMax(5, height - 5); painter->setRenderHint(QPainter::Antialiasing); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawEllipse(QPointF(x, y + height), 3, 3); painter->drawLine(x, y + 5, x, y + height - 3); pen.setWidth(2); pen.setColor(color); painter->setPen(pen); painter->drawEllipse(QPointF(x, y + height), 3, 3); painter->drawLine(x, y + 5, x, y + height - 3); } else { int x, y, width; if (where == SL_EVENT_DRAG_BELOW_ITEM) y = rect.bottom() + 1; else y = rect.top(); x = rect.left(); width = viewport->width() - rect.left() - 10; painter->setRenderHint(QPainter::Antialiasing); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawEllipse(QPointF(x + width, y), 3, 3); painter->drawLine(x, y, x + width - 3, y); pen.setWidth(2); pen.setColor(color); painter->setPen(pen); painter->drawEllipse(QPointF(x + width, y), 3, 3); painter->drawLine(x, y, x + width - 3, y); } } break; case SL_EVENT_DRAG_ON_ITEM: { option.rect = rect; rect.adjust(1, 1, -1, -1); painter->setRenderHint(QPainter::Antialiasing); int radius = qMin(8, rect.height() / 2); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawRoundedRect(rect, radius, radius); pen.setWidth(2); if (viewMode() == IconMode) { color = palette().color(QPalette::Inactive, QPalette::Highlight); pen.setColor(color); painter->setPen(pen); painter->setBrush(QBrush(color)); painter->drawRoundedRect(rect, radius, radius); QItemSelectionModel *selection = selectionModel(); if ((selection) && (selection->isSelected(index))) option.state |= QStyle::State_Selected; if (!(model()->flags(index) & Qt::ItemIsEnabled)) option.state &= ~QStyle::State_Enabled; if (option.state & QStyle::State_Enabled) option.palette.setCurrentColorGroup(QPalette::Normal); else option.palette.setCurrentColorGroup(QPalette::Disabled); itemDelegate(index)->paint(painter, option, index); } else { pen.setColor(color); painter->setPen(pen); painter->drawRoundedRect(rect, radius, radius); } } break; case SL_EVENT_DRAG_ON_VIEWPORT: { rect = viewport->rect(); rect.adjust(0, 0, -1, -1); painter->setRenderHint(QPainter::Antialiasing, false); pen.setWidth(5); pen.setColor(highlight); painter->setPen(pen); painter->drawRect(rect); pen.setWidth(3); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } break; } painter->restore(); }
void PieView::paintEvent(QPaintEvent *event) { QItemSelectionModel *selections = selectionModel(); QStyleOptionViewItem option = viewOptions(); QStyle::State state = option.state; QBrush background = option.palette.base(); QPen foreground(option.palette.color(QPalette::WindowText)); QPen textPen(option.palette.color(QPalette::Text)); QPen highlightedPen(option.palette.color(QPalette::HighlightedText)); QPainter painter(viewport()); painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(event->rect(), background); painter.setPen(foreground); // Viewport rectangles QRect pieRect = QRect(margin, margin, pieSize, pieSize); QPoint keyPoint = QPoint(totalSize - horizontalScrollBar()->value(), margin - verticalScrollBar()->value()); if (validItems > 0) { painter.save(); painter.translate(pieRect.x() - horizontalScrollBar()->value(), pieRect.y() - verticalScrollBar()->value()); painter.drawEllipse(0, 0, pieSize, pieSize); double startAngle = 0.0; int row; for (row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { double angle = 360*value/totalValue; QModelIndex colorIndex = model()->index(row, 0, rootIndex()); QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString()); if (currentIndex() == index) painter.setBrush(QBrush(color, Qt::Dense4Pattern)); else if (selections->isSelected(index)) painter.setBrush(QBrush(color, Qt::Dense3Pattern)); else painter.setBrush(QBrush(color)); painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16), int(angle*16)); startAngle += angle; } } painter.restore(); int keyNumber = 0; for (row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { QModelIndex labelIndex = model()->index(row, 0, rootIndex()); QStyleOptionViewItem option = viewOptions(); option.rect = visualRect(labelIndex); if (selections->isSelected(labelIndex)) option.state |= QStyle::State_Selected; if (currentIndex() == labelIndex) option.state |= QStyle::State_HasFocus; itemDelegate()->paint(&painter, option, labelIndex); keyNumber++; } } } }