void NodeTypesDelegate::updateItemWidgets(const QList< QWidget* > widgets, const QStyleOptionViewItem& option, const QPersistentModelIndex& index) const { // widgets: // ColorButton | Title | ID if (!index.isValid()) { return; } Q_ASSERT(widgets.size() == 4); KColorButton *colorButton = qobject_cast<KColorButton*>(widgets.at(0)); QLineEdit *title = qobject_cast<QLineEdit*>(widgets.at(1)); QLabel *id = qobject_cast<QLabel*>(widgets.at(2)); QToolButton *propertiesButton = qobject_cast<QToolButton*>(widgets.at(3)); Q_ASSERT(title); Q_ASSERT(colorButton); Q_ASSERT(id); Q_ASSERT(propertiesButton); colorButton->setColor(index.data(NodeTypeModel::ColorRole).value<QColor>()); title->setText(index.data(NodeTypeModel::TitleRole).toString()); id->setText(index.data(NodeTypeModel::IdRole).toString()); QRect outerRect(0, 0, option.rect.width(), option.rect.height()); QRect contentRect = outerRect.adjusted(m_hPadding, m_vPadding, -m_hPadding, -m_vPadding); int colorButtonLeftMargin = contentRect.left(); int colorButtonTopMargin = (outerRect.height() - colorButton->height()) / 2; colorButton->move(colorButtonLeftMargin, colorButtonTopMargin); int titleLeftMargin = colorButtonLeftMargin + colorButton->width() + 10; int titleTopMargin = (outerRect.height() - title->height()) / 2; title->move(titleLeftMargin, titleTopMargin); // construct remaining from right to left int propertiesLeftMargin = contentRect.right() - propertiesButton->width() - m_hPadding; int propertiesTopMargin = (outerRect.height() - propertiesButton->height()) / 2; propertiesButton->move(propertiesLeftMargin, propertiesTopMargin); int idLeftMargin = propertiesLeftMargin - id->width() - 10; int idTopMargin = (outerRect.height() - id->height()) / 2; id->move(idLeftMargin, idTopMargin); // title gets remaining space title->setFixedWidth(qMax(0, idLeftMargin - titleLeftMargin - 10)); }
void ItemsViewDelegate::updateItemWidgets(const QList<QWidget*> widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const { const QSortFilterProxyModel * model = qobject_cast<const QSortFilterProxyModel*>(index.model()); if (model == NULL) { return; } const ItemsModel * realmodel = qobject_cast<const ItemsModel*>(model->sourceModel()); if (realmodel == NULL || !index.isValid()) { return; } // setup the install button int margin = option.fontMetrics.height() / 2; int right = option.rect.width(); //int bottom = option.rect.height(); QSize size(option.fontMetrics.height() * 7, widgets.at(kInstall)->sizeHint().height()); QLabel * infoLabel = qobject_cast<QLabel*>(widgets.at(kLabel)); infoLabel->setWordWrap(true); if (infoLabel != NULL) { if (realmodel->hasPreviewImages()) { // move the text right by kPreviewWidth + margin pixels to fit the preview infoLabel->move(kPreviewWidth + margin * 2, 0); infoLabel->resize(QSize(option.rect.width() - kPreviewWidth - (margin * 6) - size.width(), option.fontMetrics.height() * 7)); } else { infoLabel->move(margin, 0); infoLabel->resize(QSize(option.rect.width() - (margin * 4) - size.width(), option.fontMetrics.height() * 7)); } QString text = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">p, li { white-space: pre-wrap; margin:0 0 0 0;}\n" "</style></head><body><p><b>" + index.data(ItemsModel::kNameRole).toString() + "</b></p>\n"; QString summary = "<p>" + option.fontMetrics.elidedText(index.data(ItemsModel::kSummary).toString(), Qt::ElideRight, infoLabel->width() * 3) + "</p>\n"; text += summary; QString authorName = index.data(ItemsModel::kAuthorName).toString(); QString email = index.data(ItemsModel::kAuthorEmail).toString(); if (!authorName.isEmpty()) { if (email.isEmpty()) { text += "<p><i>" + authorName + "</i></p>\n"; } else { text += "<p><i>" + authorName + "</i> <a href=\"mailto:" + email + "\">" + email + "</a></p>\n"; } } unsigned int downloads = index.data(ItemsModel::kDownloads).toUInt(); text += downloads == 0 ? i18n("<p>No Downloads</p>") : i18n("<p>Downloads: %1</p>\n", downloads); text += "</body></html>"; text.replace("[b]", "<b>"); text.replace("[/b]", "</b>"); text.replace("[i]", "<i>"); text.replace("[/i]", "</i>"); text.replace("[u]", "<i>"); text.replace("[/u]", "</i>"); text.remove("[url]"); text.remove("[/url]"); text.replace("\\\'", "\'"); infoLabel->setText(text.simplified()); } QToolButton * button = qobject_cast<QToolButton*>(widgets.at(kInstall)); if (button != NULL) { Entry::Status status = Entry::Status(model->data(index, ItemsModel::kStatus).toUInt()); //if (!button->menu()) { // button->setMenu(InstallMenu(button, status)); // button->setIconSize(QSize(16, 16)); button->resize(size); //} button->move(right - button->width() - margin, option.rect.height() / 2 - button->height()); button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //button->setPopupMode(QToolButton::MenuButtonPopup); // validate our assumptions //Q_ASSERT(button->menu()); //Q_ASSERT(button->menu()->actions().count() == 2); // get the two actions //QAction * action_install = button->menu()->actions()[0]; //QAction * action_uninstall = button->menu()->actions()[1]; switch (status) { case Entry::Installed: button->setText(i18n("Uninstall")); //action_install->setVisible(false); //action_uninstall->setVisible(true); button->setIcon(QIcon(m_statusicons[Entry::Deleted])); break; case Entry::Updateable: button->setText(i18n("Update")); //action_uninstall->setVisible(false); //action_install->setText(i18n("Update")); //action_install->setVisible(true); //action_install->setIcon(QIcon(m_statusicons[Entry::Updateable])); button->setIcon(QIcon(m_statusicons[Entry::Updateable])); break; case Entry::Deleted: /// @todo Set different button text when string freeze is over? "Install again" button->setText(i18n("Install")); //action_uninstall->setVisible(false); //action_install->setText(i18n("Install")); //action_install->setVisible(true); //action_install->setIcon(QIcon(m_statusicons[Entry::Installed])); button->setIcon(QIcon(m_statusicons[Entry::Installed])); break; default: button->setText(i18n("Install")); //action_uninstall->setVisible(false); //action_install->setVisible(true); //action_install->setIcon(QIcon(m_statusicons[Entry::Installed])); button->setIcon(QIcon(m_statusicons[Entry::Installed])); } } QLabel * ratingLabel = qobject_cast<QLabel*>(widgets.at(kRating)); if (ratingLabel != NULL) { ratingLabel->setText(i18n("Rating: %1", model->data(index, ItemsModel::kRating).toString())); // put the rating label below the install button ratingLabel->move(right - button->width() - margin, option.rect.height() / 2 + button->height()/2); ratingLabel->resize(size); } }
/* FIXME make this less of a hack. */ QRect TestCalendarWidget::visualRect(QString const &item) const { TestWidgetsLog() << item << "my geometry is" << geometry(); QRect ret; QAbstractItemView *view = q->findChild<QAbstractItemView*>(); QtUiTest::ListWidget *calendarView = qtuitest_cast<QtUiTest::ListWidget*>(view); if (!calendarView) { return ret; } ret = calendarView->visualRect(item); if (!ret.isNull()) { ret.moveTopLeft( q->mapFromGlobal( view->mapToGlobal(ret.topLeft()) ) ); TestWidgetsLog() << item << "is a visible day at" << ret; return ret; } QToolButton *yearButton = 0; QToolButton *monthButton = 0; QSpinBox *yearSpin = q->findChild<QSpinBox*>(); QMenu *monthMenu = 0; QList<QToolButton*> blist = q->findChildren<QToolButton*>(); foreach(QToolButton *b, blist) { if (!monthButton && (monthMenu = b->menu())) { monthButton = b; } if (!b->menu()) { yearButton = b; } } TestWidgetsLog() << "monthButton" << monthButton << "yearButton" << yearButton; TestWidgetsLog() << "item" << item << "monthMenu" << monthMenu; if (yearButton && yearButton->isVisible() && yearButton->text() == item) { QPoint p = q->mapFromGlobal( yearButton->mapToGlobal(QPoint(yearButton->width()+5, yearButton->height()/2)) ); ret = QRect(p.x() - 2, p.y() - 2, 5, 5); TestWidgetsLog() << "click near yearbutton"; } else if (yearSpin && yearSpin->isVisible() && yearSpin->value() == item.toInt()) { TestWidgetsLog() << "confirm spinbox"; QPoint p = q->mapFromGlobal( yearSpin->mapToGlobal(QPoint(yearSpin->width()+5, yearSpin->height()/2)) ); ret = QRect(p.x() - 2, p.y() - 2, 5, 5); } else if (monthButton && monthButton->isVisible() && monthButton->text() == item) { QPoint p = q->mapFromGlobal( monthButton->mapToGlobal(QPoint(-5, monthButton->height()/2)) ); ret = QRect(p.x() - 2, p.y() - 2, 5, 5); TestWidgetsLog() << "click near monthbutton"; } else if (monthMenu && monthMenu->isVisible() && qtuitest_cast<QtUiTest::ListWidget*>(monthMenu) && qtuitest_cast<QtUiTest::ListWidget*>(monthMenu)->list().contains(item)) { ret = qtuitest_cast<QtUiTest::ListWidget*>(monthMenu)->visualRect(item); ret.moveTopLeft( q->mapFromGlobal( monthMenu->mapToGlobal(ret.topLeft()) ) ); TestWidgetsLog() << "click on monthmenu"; } else { do { QStringList items = list(); if (items.contains(item)) { ret = QRect(-1, -1, 1, 1); ret.moveTopLeft( q->mapFromGlobal(QPoint(-1,-1)) ); break; } foreach (QString s, items) { if (!s.startsWith(GetListRegExp)) continue; QRegExp re(s.mid(GetListRegExp.length())); if (re.exactMatch(item)) { ret = QRect(-1, -1, 1, 1); ret.moveTopLeft( q->mapFromGlobal(QPoint(-1,-1)) ); break; } } if (!ret.isNull()) break; } while(0); } TestWidgetsLog() << "returning rect" << ret; return ret; }