void OperationsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { const QVariant value = index.data(Qt::EditRole); if (value.type() == QVariant::StringList) { QListWidget *lw = qobject_cast<QListWidget *>(editor); const auto items = lw->findItems(index.data(Qt::DisplayRole).toString(), Qt::MatchExactly); if (!items.empty()) lw->setCurrentItem(items.first()); else lw->setCurrentItem(lw->item(0)); const int extraWidth = 25; const int extraHeight = 6; lw->setMinimumWidth(lw->sizeHintForColumn(0) + extraWidth); // to prevent possible hiding bottom part of the list const int h = lw->count() * (lw->visualItemRect(lw->currentItem()).height() + extraHeight); const int y = (lw->parentWidget() && (lw->parentWidget()->rect().bottom() < lw->y() + h)) ? lw->parentWidget()->rect().bottom() - h - extraHeight : lw->y(); lw->setGeometry(lw->x(), y, lw->width(), h); // now lw can be partially hidden behind the tree view // if tree view has small rect, so set parent of lw // to app window and map coordinates accordingly to leave lw in place const auto globalCoord = lw->parentWidget()->mapToGlobal(lw->geometry().topLeft()); lw->setParent(appWindow); const auto newLocalCoord = appWindow->mapFromGlobal(globalCoord); lw->setGeometry(newLocalCoord.x(), newLocalCoord.y(), lw->width(), h); } else // single value QStyledItemDelegate::setEditorData(editor, index); }
int KMessageBox::createKMessageBox(KDialog *dialog, const QIcon &icon, const QString &text, const QStringList &strlist, const QString &ask, bool *checkboxReturn, Options options, const QString &details, QMessageBox::Icon notifyType) { QWidget *mainWidget = new QWidget(dialog); QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget); mainLayout->setSpacing(KDialog::spacingHint() * 2); // provide extra spacing mainLayout->setMargin(0); QHBoxLayout *hLayout = new QHBoxLayout(); hLayout->setMargin(0); hLayout->setSpacing(-1); // use default spacing mainLayout->addLayout(hLayout,5); QLabel *iconLabel = new QLabel(mainWidget); if (!icon.isNull()) { QStyleOption option; option.initFrom(mainWidget); iconLabel->setPixmap(icon.pixmap(mainWidget->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, mainWidget))); } QVBoxLayout *iconLayout = new QVBoxLayout(); iconLayout->addStretch(1); iconLayout->addWidget(iconLabel); iconLayout->addStretch(5); hLayout->addLayout(iconLayout,0); hLayout->addSpacing(KDialog::spacingHint()); QLabel *messageLabel = new QLabel(text, mainWidget); messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink); Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard; if (options & KMessageBox::AllowLink) { flags |= Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard; } messageLabel->setTextInteractionFlags(flags); QRect desktop = KGlobalSettings::desktopGeometry(dialog); bool usingSqueezedTextLabel = false; if (messageLabel->sizeHint().width() > desktop.width() * 0.5) { // enable automatic wrapping of messages which are longer than 50% of screen width messageLabel->setWordWrap(true); // display a text widget with scrollbar if still too wide usingSqueezedTextLabel = messageLabel->sizeHint().width() > desktop.width() * 0.85; if (usingSqueezedTextLabel) { delete messageLabel; messageLabel = new KSqueezedTextLabel(text, mainWidget); messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink); messageLabel->setTextInteractionFlags(flags); } } QPalette messagePal(messageLabel->palette()); messagePal.setColor(QPalette::Window, Qt::transparent); messageLabel->setPalette(messagePal); bool usingScrollArea=desktop.height() / 3 < messageLabel->sizeHint().height(); if (usingScrollArea) { QScrollArea* messageScrollArea = new QScrollArea(mainWidget); messageScrollArea->setWidget(messageLabel); messageScrollArea->setFrameShape(QFrame::NoFrame); messageScrollArea->setWidgetResizable(true); QPalette scrollPal(messageScrollArea->palette()); scrollPal.setColor(QPalette::Window, Qt::transparent); messageScrollArea->viewport()->setPalette(scrollPal); hLayout->addWidget(messageScrollArea,5); } else hLayout->addWidget(messageLabel,5); const bool usingListWidget=!strlist.isEmpty(); if (usingListWidget) { // enable automatic wrapping since the listwidget has already a good initial width messageLabel->setWordWrap(true); QListWidget *listWidget = new QListWidget(mainWidget); listWidget->addItems(strlist); QStyleOptionViewItem styleOption; styleOption.initFrom(listWidget); QFontMetrics fm(styleOption.font); int w = listWidget->width(); Q_FOREACH(const QString &str, strlist) { w = qMax(w, fm.width(str)); } const int borderWidth = listWidget->width() - listWidget->viewport()->width() + listWidget->verticalScrollBar()->height(); w += borderWidth; if (w > desktop.width() * 0.85) { // limit listWidget size to 85% of screen width w = qRound(desktop.width() * 0.85); } listWidget->setMinimumWidth(w); mainLayout->addWidget(listWidget,usingScrollArea?10:50); listWidget->setSelectionMode(QListWidget::NoSelection); messageLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum); }