| 1 |
/* This file is part of the KDE Libraries |
| 2 |
* SPDX-FileCopyrightText: 1998 Thomas Tanghus ([email protected]) |
| 3 |
* Additions 1999-2000 by Espen Sand ([email protected]) |
| 4 |
* by Holger Freyther <[email protected]> |
| 5 |
* 2005-2009 by Olivier Goffart (ogoffart at kde.org) |
| 6 |
* |
| 7 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
// clazy:excludeall=qstring-arg |
| 11 |
#include "KoDialog.h" |
| 12 |
#include "KoDialog_p.h" |
| 13 |
|
| 14 |
#include <QApplication> |
| 15 |
#include <QDialogButtonBox> |
| 16 |
#include <QHBoxLayout> |
| 17 |
#include <QHideEvent> |
| 18 |
#include <QPointer> |
| 19 |
#include <QStyle> |
| 20 |
#include <QTimer> |
| 21 |
#include <QVBoxLayout> |
| 22 |
#include <QWhatsThis> |
| 23 |
#include <QDebug> |
| 24 |
#include <QPushButton> |
| 25 |
|
| 26 |
#include <KConfig> |
| 27 |
#include <KLocalizedString> |
| 28 |
|
| 29 |
#include <KSeparator> |
| 30 |
#include <KStandardGuiItem> |
| 31 |
#include <KHelpClient> |
| 32 |
#include <KUrlLabel> |
| 33 |
#include <KWindowConfig> |
| 34 |
|
| 35 |
void KoDialogPrivate::setupLayout() |
| 36 |
{ |
| 37 |
Q_Q(KoDialog); |
| 38 |
if (!dirty) { |
| 39 |
QMetaObject::invokeMethod(q, "queuedLayoutUpdate", Qt::QueuedConnection); |
| 40 |
dirty = true; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
void KoDialogPrivate::queuedLayoutUpdate() |
| 45 |
{ |
| 46 |
if (!dirty) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
dirty = false; |
| 51 |
|
| 52 |
Q_Q(KoDialog); |
| 53 |
|
| 54 |
// Don't lose the focus widget when re-creating the layout. |
| 55 |
// Testcase: KOrganizer's "Select Categories" dialog |
| 56 |
QPointer<QWidget> focusWidget = mMainWidget ? mMainWidget->focusWidget() : nullptr; |
| 57 |
|
| 58 |
if (q->layout() && q->layout() != mTopLayout) { |
| 59 |
qWarning() << q->metaObject()->className() << "created with a layout; don't do that, KoDialog takes care of it, use mainWidget or setMainWidget instead"; |
| 60 |
delete q->layout(); |
| 61 |
} |
| 62 |
|
| 63 |
delete mTopLayout; |
| 64 |
|
| 65 |
if (mButtonOrientation == Qt::Horizontal) { |
| 66 |
mTopLayout = new QVBoxLayout(q); |
| 67 |
} else { |
| 68 |
mTopLayout = new QHBoxLayout(q); |
| 69 |
} |
| 70 |
|
| 71 |
if (mUrlHelp) { |
| 72 |
mTopLayout->addWidget(mUrlHelp, 0, Qt::AlignRight); |
| 73 |
} |
| 74 |
|
| 75 |
if (mMainWidget) { |
| 76 |
mTopLayout->addWidget(mMainWidget, 10); |
| 77 |
} |
| 78 |
|
| 79 |
if (mDetailsWidget) { |
| 80 |
mTopLayout->addWidget(mDetailsWidget); |
| 81 |
} |
| 82 |
|
| 83 |
if (mActionSeparator) { |
| 84 |
mTopLayout->addWidget(mActionSeparator); |
| 85 |
} |
| 86 |
|
| 87 |
if (mButtonBox) { |
| 88 |
mButtonBox->setOrientation(mButtonOrientation); |
| 89 |
mTopLayout->addWidget(mButtonBox); |
| 90 |
} |
| 91 |
|
| 92 |
if (focusWidget) { |
| 93 |
focusWidget->setFocus(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void KoDialogPrivate::appendButton(KoDialog::ButtonCode key, const KGuiItem &item) |
| 98 |
{ |
| 99 |
Q_Q(KoDialog); |
| 100 |
|
| 101 |
QDialogButtonBox::ButtonRole role = QDialogButtonBox::InvalidRole; |
| 102 |
switch (key) { |
| 103 |
case KoDialog::Help: |
| 104 |
case KoDialog::Details: |
| 105 |
role = QDialogButtonBox::HelpRole; |
| 106 |
break; |
| 107 |
case KoDialog::Default: |
| 108 |
case KoDialog::Reset: |
| 109 |
role = QDialogButtonBox::ResetRole; |
| 110 |
break; |
| 111 |
case KoDialog::Ok: |
| 112 |
role = QDialogButtonBox::AcceptRole; |
| 113 |
break; |
| 114 |
case KoDialog::Apply: |
| 115 |
role = QDialogButtonBox::ApplyRole; |
| 116 |
break; |
| 117 |
case KoDialog::Try: |
| 118 |
case KoDialog::Close: |
| 119 |
case KoDialog::Cancel: |
| 120 |
role = QDialogButtonBox::RejectRole; |
| 121 |
break; |
| 122 |
case KoDialog::User1: |
| 123 |
case KoDialog::User2: |
| 124 |
case KoDialog::User3: |
| 125 |
role = QDialogButtonBox::ActionRole; |
| 126 |
break; |
| 127 |
default: |
| 128 |
role = QDialogButtonBox::InvalidRole; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
if (role == QDialogButtonBox::InvalidRole) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
QPushButton *button = new QPushButton; |
| 137 |
KGuiItem::assign(button, item); |
| 138 |
mButtonBox->addButton(button, role); |
| 139 |
|
| 140 |
mButtonList.insert(key, button); |
| 141 |
mButtonSignalMapper.setMapping(button, key); |
| 142 |
|
| 143 |
QObject::connect(button, SIGNAL(clicked()), |
| 144 |
&mButtonSignalMapper, SLOT(map())); |
| 145 |
|
| 146 |
if (key == mDefaultButton) { |
| 147 |
// Now that it exists, set it as default |
| 148 |
q->setDefaultButton(mDefaultButton); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void KoDialogPrivate::init(KoDialog *q) |
| 153 |
{ |
| 154 |
q_ptr = q; |
| 155 |
|
| 156 |
dirty = false; |
| 157 |
|
| 158 |
q->setButtons(KoDialog::Ok | KoDialog::Cancel); |
| 159 |
q->setDefaultButton(KoDialog::Ok); |
| 160 |
|
| 161 |
q->connect(&mButtonSignalMapper, &QSignalMapper::mappedInt, q, &KoDialog::slotButtonClicked); |
| 162 |
|
| 163 |
q->setPlainCaption(qApp->applicationDisplayName()); // set appropriate initial window title for case it gets not set later |
| 164 |
} |
| 165 |
|
| 166 |
void KoDialogPrivate::helpLinkClicked() |
| 167 |
{ |
| 168 |
q_ptr->slotButtonClicked(KoDialog::Help); |
| 169 |
} |
| 170 |
|
| 171 |
KoDialog::KoDialog(QWidget *parent, Qt::WindowFlags flags) |
| 172 |
: QDialog(parent, flags) |
| 173 |
, d_ptr(new KoDialogPrivate) |
| 174 |
{ |
| 175 |
d_ptr->init(this); |
| 176 |
} |
| 177 |
|
| 178 |
KoDialog::KoDialog(KoDialogPrivate &dd, QWidget *parent, Qt::WindowFlags flags) |
| 179 |
: QDialog(parent, flags) |
| 180 |
, d_ptr(&dd) |
| 181 |
{ |
| 182 |
d_ptr->init(this); |
| 183 |
} |
| 184 |
|
| 185 |
KoDialog::~KoDialog() |
| 186 |
{ |
| 187 |
delete d_ptr; |
| 188 |
} |
| 189 |
|
| 190 |
void KoDialog::setButtons(ButtonCodes buttonMask) |
| 191 |
{ |
| 192 |
Q_D(KoDialog); |
| 193 |
if (d->mButtonBox) { |
| 194 |
d->mButtonList.clear(); |
| 195 |
|
| 196 |
delete d->mButtonBox; |
| 197 |
d->mButtonBox = nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
if (buttonMask & Cancel) { |
| 201 |
buttonMask &= ~Close; |
| 202 |
} |
| 203 |
|
| 204 |
if (buttonMask & Apply) { |
| 205 |
buttonMask &= ~Try; |
| 206 |
} |
| 207 |
|
| 208 |
if (buttonMask & Details) { |
| 209 |
buttonMask &= ~Default; |
| 210 |
} |
| 211 |
|
| 212 |
if (buttonMask == None) { |
| 213 |
d->setupLayout(); |
| 214 |
return; // When we want no button box |
| 215 |
} |
| 216 |
|
| 217 |
d->mEscapeButton = (buttonMask & Cancel) ? Cancel : Close; |
| 218 |
d->mButtonBox = new QDialogButtonBox(this); |
| 219 |
|
| 220 |
if (buttonMask & Help) { |
| 221 |
d->appendButton(Help, KStandardGuiItem::help()); |
| 222 |
} |
| 223 |
if (buttonMask & Default) { |
| 224 |
d->appendButton(Default, KStandardGuiItem::defaults()); |
| 225 |
} |
| 226 |
if (buttonMask & Reset) { |
| 227 |
d->appendButton(Reset, KStandardGuiItem::reset()); |
| 228 |
} |
| 229 |
if (buttonMask & User3) { |
| 230 |
d->appendButton(User3, KGuiItem()); |
| 231 |
} |
| 232 |
if (buttonMask & User2) { |
| 233 |
d->appendButton(User2, KGuiItem()); |
| 234 |
} |
| 235 |
if (buttonMask & User1) { |
| 236 |
d->appendButton(User1, KGuiItem()); |
| 237 |
} |
| 238 |
if (buttonMask & Ok) { |
| 239 |
d->appendButton(Ok, KStandardGuiItem::ok()); |
| 240 |
} |
| 241 |
if (buttonMask & Apply) { |
| 242 |
d->appendButton(Apply, KStandardGuiItem::apply()); |
| 243 |
} |
| 244 |
if (buttonMask & Try) { |
| 245 |
d->appendButton(Try, KGuiItem(i18n("&Try"))); |
| 246 |
} |
| 247 |
if (buttonMask & Cancel) { |
| 248 |
d->appendButton(Cancel, KStandardGuiItem::cancel()); |
| 249 |
} |
| 250 |
if (buttonMask & Close) { |
| 251 |
d->appendButton(Close, KStandardGuiItem::close()); |
| 252 |
} |
| 253 |
if (buttonMask & Details) { |
| 254 |
d->appendButton(Details, KGuiItem(QString(), QStringLiteral("help-about"))); |
| 255 |
setDetailsWidgetVisible(false); |
| 256 |
} |
| 257 |
|
| 258 |
d->setupLayout(); |
| 259 |
} |
| 260 |
|
| 261 |
void KoDialog::setButtonsOrientation(Qt::Orientation orientation) |
| 262 |
{ |
| 263 |
Q_D(KoDialog); |
| 264 |
if (d->mButtonOrientation != orientation) { |
| 265 |
d->mButtonOrientation = orientation; |
| 266 |
|
| 267 |
if (d->mActionSeparator) { |
| 268 |
d->mActionSeparator->setOrientation(d->mButtonOrientation); |
| 269 |
} |
| 270 |
|
| 271 |
if (d->mButtonOrientation == Qt::Vertical) { |
| 272 |
enableLinkedHelp(false); // 2000-06-18 Espen: No support for this yet. |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
void KoDialog::setEscapeButton(ButtonCode id) |
| 278 |
{ |
| 279 |
d_func()->mEscapeButton = id; |
| 280 |
} |
| 281 |
|
| 282 |
void KoDialog::setDefaultButton(ButtonCode newDefaultButton) |
| 283 |
{ |
| 284 |
Q_D(KoDialog); |
| 285 |
|
| 286 |
if (newDefaultButton == None) { |
| 287 |
newDefaultButton = NoDefault; // #148969 |
| 288 |
} |
| 289 |
|
| 290 |
const KoDialog::ButtonCode oldDefault = defaultButton(); |
| 291 |
|
| 292 |
bool oldDefaultHadFocus = false; |
| 293 |
|
| 294 |
if (oldDefault != NoDefault) { |
| 295 |
QPushButton *old = button(oldDefault); |
| 296 |
if (old) { |
| 297 |
oldDefaultHadFocus = (focusWidget() == old); |
| 298 |
old->setDefault(false); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if (newDefaultButton != NoDefault) { |
| 303 |
QPushButton *b = button(newDefaultButton); |
| 304 |
if (b) { |
| 305 |
b->setDefault(true); |
| 306 |
if (focusWidget() == nullptr || oldDefaultHadFocus) { |
| 307 |
// No widget had focus yet, or the old default button had |
| 308 |
// -> ok to give focus to the new default button, so that it's |
| 309 |
// really default (Enter triggers it). |
| 310 |
// But we don't do this if the kdialog user gave focus to a |
| 311 |
// specific widget in the dialog. |
| 312 |
b->setFocus(); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
d->mDefaultButton = newDefaultButton; |
| 317 |
Q_ASSERT(defaultButton() == newDefaultButton); |
| 318 |
} |
| 319 |
|
| 320 |
KoDialog::ButtonCode KoDialog::defaultButton() const |
| 321 |
{ |
| 322 |
Q_D(const KoDialog); |
| 323 |
QHashIterator<int, QPushButton *> it(d->mButtonList); |
| 324 |
while (it.hasNext()) { |
| 325 |
it.next(); |
| 326 |
if (it.value()->isDefault()) { |
| 327 |
return (ButtonCode)it.key(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return d->mDefaultButton; |
| 332 |
} |
| 333 |
|
| 334 |
void KoDialog::setMainWidget(QWidget *widget) |
| 335 |
{ |
| 336 |
Q_D(KoDialog); |
| 337 |
if (d->mMainWidget == widget) { |
| 338 |
return; |
| 339 |
} |
| 340 |
d->mMainWidget = widget; |
| 341 |
if (d->mMainWidget && d->mMainWidget->layout()) { |
| 342 |
// Avoid double-margin problem |
| 343 |
d->mMainWidget->layout()->setContentsMargins(0, 0, 0, 0); |
| 344 |
} |
| 345 |
d->setupLayout(); |
| 346 |
} |
| 347 |
|
| 348 |
QWidget *KoDialog::mainWidget() |
| 349 |
{ |
| 350 |
Q_D(KoDialog); |
| 351 |
if (!d->mMainWidget) { |
| 352 |
setMainWidget(new QWidget(this)); |
| 353 |
} |
| 354 |
return d->mMainWidget; |
| 355 |
} |
| 356 |
|
| 357 |
QSize KoDialog::sizeHint() const |
| 358 |
{ |
| 359 |
Q_D(const KoDialog); |
| 360 |
|
| 361 |
if (!d->mMinSize.isEmpty()) { |
| 362 |
return d->mMinSize.expandedTo(minimumSizeHint()) + d->mIncSize; |
| 363 |
} else { |
| 364 |
if (d->dirty) { |
| 365 |
const_cast<KoDialogPrivate *>(d)->queuedLayoutUpdate(); |
| 366 |
} |
| 367 |
return QDialog::sizeHint() + d->mIncSize; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
QSize KoDialog::minimumSizeHint() const |
| 372 |
{ |
| 373 |
Q_D(const KoDialog); |
| 374 |
|
| 375 |
if (d->dirty) { |
| 376 |
const_cast<KoDialogPrivate *>(d)->queuedLayoutUpdate(); |
| 377 |
} |
| 378 |
return QDialog::minimumSizeHint() + d->mIncSize; |
| 379 |
} |
| 380 |
|
| 381 |
// |
| 382 |
// Grab QDialogs keypresses if non-modal. |
| 383 |
// |
| 384 |
void KoDialog::keyPressEvent(QKeyEvent *event) |
| 385 |
{ |
| 386 |
Q_D(KoDialog); |
| 387 |
if (event->modifiers() == 0) { |
| 388 |
if (event->key() == Qt::Key_F1) { |
| 389 |
QPushButton *button = this->button(Help); |
| 390 |
|
| 391 |
if (button) { |
| 392 |
button->animateClick(); |
| 393 |
event->accept(); |
| 394 |
return; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if (event->key() == Qt::Key_Escape) { |
| 399 |
QPushButton *button = this->button(d->mEscapeButton); |
| 400 |
|
| 401 |
if (button) { |
| 402 |
button->animateClick(); |
| 403 |
event->accept(); |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
} |
| 408 |
} else if (event->key() == Qt::Key_F1 && event->modifiers() == Qt::ShiftModifier) { |
| 409 |
QWhatsThis::enterWhatsThisMode(); |
| 410 |
event->accept(); |
| 411 |
return; |
| 412 |
} else if (event->modifiers() == Qt::ControlModifier && |
| 413 |
(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)) { |
| 414 |
// accept the dialog when Ctrl-Return is pressed |
| 415 |
QPushButton *button = this->button(Ok); |
| 416 |
|
| 417 |
if (button) { |
| 418 |
button->animateClick(); |
| 419 |
event->accept(); |
| 420 |
return; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
QDialog::keyPressEvent(event); |
| 425 |
} |
| 426 |
|
| 427 |
QString KoDialog::makeStandardCaption(const QString &userCaption, |
| 428 |
QWidget *window, |
| 429 |
CaptionFlags flags) |
| 430 |
{ |
| 431 |
Q_UNUSED(window); |
| 432 |
QString caption = qApp->applicationDisplayName(); |
| 433 |
QString captionString = userCaption.isEmpty() ? caption : userCaption; |
| 434 |
|
| 435 |
// If the document is modified, add '[modified]'. |
| 436 |
if (flags & ModifiedCaption) { |
| 437 |
captionString += QString::fromUtf8(" [") + i18n("modified") + QString::fromUtf8("]"); |
| 438 |
} |
| 439 |
|
| 440 |
if (!userCaption.isEmpty()) { |
| 441 |
// Add the application name if: |
| 442 |
// User asked for it, it's not a duplication and the app name (caption()) is not empty |
| 443 |
if (flags & AppNameCaption && |
| 444 |
!caption.isEmpty() && |
| 445 |
!userCaption.endsWith(caption)) { |
| 446 |
// TODO: check to see if this is a transient/secondary window before trying to add the app name |
| 447 |
// on platforms that need this |
| 448 |
captionString += i18nc("Document/application separator in titlebar", " – ") + caption; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return captionString; |
| 453 |
} |
| 454 |
|
| 455 |
void KoDialog::setCaption(const QString &_caption) |
| 456 |
{ |
| 457 |
const QString caption = makeStandardCaption(_caption, this); |
| 458 |
setPlainCaption(caption); |
| 459 |
} |
| 460 |
|
| 461 |
void KoDialog::setCaption(const QString &caption, bool modified) |
| 462 |
{ |
| 463 |
CaptionFlags flags = HIGCompliantCaption; |
| 464 |
|
| 465 |
// ### Qt5 TODO: port to [*], see QWidget::setWindowFilePath |
| 466 |
if (modified) { |
| 467 |
flags |= ModifiedCaption; |
| 468 |
} |
| 469 |
|
| 470 |
setPlainCaption(makeStandardCaption(caption, this, flags)); |
| 471 |
} |
| 472 |
|
| 473 |
void KoDialog::setPlainCaption(const QString &caption) |
| 474 |
{ |
| 475 |
if (QWidget *win = window()) { |
| 476 |
win->setWindowTitle(caption); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
void KoDialog::resizeLayout(QWidget *widget, int margin, int spacing) //static |
| 481 |
{ |
| 482 |
if (widget->layout()) { |
| 483 |
resizeLayout(widget->layout(), margin, spacing); |
| 484 |
} |
| 485 |
|
| 486 |
if (widget->children().count() > 0) { |
| 487 |
const QList<QObject *> list = widget->children(); |
| 488 |
for (QObject *object : list) { |
| 489 |
if (object->isWidgetType()) { |
| 490 |
resizeLayout((QWidget *)object, margin, spacing); |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void KoDialog::resizeLayout(QLayout *layout, int margin, int spacing) //static |
| 497 |
{ |
| 498 |
QLayoutItem *child; |
| 499 |
int pos = 0; |
| 500 |
|
| 501 |
while ((child = layout->itemAt(pos))) { |
| 502 |
if (child->layout()) { |
| 503 |
resizeLayout(child->layout(), margin, spacing); |
| 504 |
} |
| 505 |
|
| 506 |
++pos; |
| 507 |
} |
| 508 |
|
| 509 |
if (layout->layout()) { |
| 510 |
layout->layout()->setContentsMargins(margin, margin, margin, margin); |
| 511 |
layout->layout()->setSpacing(spacing); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
void KoDialog::showButtonSeparator(bool state) |
| 516 |
{ |
| 517 |
Q_D(KoDialog); |
| 518 |
if ((d->mActionSeparator != nullptr) == state) { |
| 519 |
return; |
| 520 |
} |
| 521 |
if (state) { |
| 522 |
if (d->mActionSeparator) { |
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
d->mActionSeparator = new KSeparator(this); |
| 527 |
d->mActionSeparator->setOrientation(d->mButtonOrientation); |
| 528 |
} else { |
| 529 |
delete d->mActionSeparator; |
| 530 |
d->mActionSeparator = nullptr; |
| 531 |
} |
| 532 |
|
| 533 |
d->setupLayout(); |
| 534 |
} |
| 535 |
|
| 536 |
void KoDialog::setInitialSize(const QSize &size) |
| 537 |
{ |
| 538 |
d_func()->mMinSize = size; |
| 539 |
adjustSize(); |
| 540 |
} |
| 541 |
|
| 542 |
void KoDialog::incrementInitialSize(const QSize &size) |
| 543 |
{ |
| 544 |
d_func()->mIncSize = size; |
| 545 |
adjustSize(); |
| 546 |
} |
| 547 |
|
| 548 |
QPushButton *KoDialog::button(ButtonCode id) const |
| 549 |
{ |
| 550 |
Q_D(const KoDialog); |
| 551 |
return d->mButtonList.value(id, nullptr); |
| 552 |
} |
| 553 |
|
| 554 |
void KoDialog::enableButton(ButtonCode id, bool state) |
| 555 |
{ |
| 556 |
QPushButton *button = this->button(id); |
| 557 |
if (button) { |
| 558 |
button->setEnabled(state); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
bool KoDialog::isButtonEnabled(ButtonCode id) const |
| 563 |
{ |
| 564 |
QPushButton *button = this->button(id); |
| 565 |
if (button) { |
| 566 |
return button->isEnabled(); |
| 567 |
} |
| 568 |
|
| 569 |
return false; |
| 570 |
} |
| 571 |
|
| 572 |
void KoDialog::enableButtonOk(bool state) |
| 573 |
{ |
| 574 |
enableButton(Ok, state); |
| 575 |
} |
| 576 |
|
| 577 |
void KoDialog::enableButtonApply(bool state) |
| 578 |
{ |
| 579 |
enableButton(Apply, state); |
| 580 |
} |
| 581 |
|
| 582 |
void KoDialog::enableButtonCancel(bool state) |
| 583 |
{ |
| 584 |
enableButton(Cancel, state); |
| 585 |
} |
| 586 |
|
| 587 |
void KoDialog::showButton(ButtonCode id, bool state) |
| 588 |
{ |
| 589 |
QPushButton *button = this->button(id); |
| 590 |
if (button) { |
| 591 |
state ? button->show() : button->hide(); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
void KoDialog::setButtonGuiItem(ButtonCode id, const KGuiItem &item) |
| 596 |
{ |
| 597 |
QPushButton *button = this->button(id); |
| 598 |
if (!button) { |
| 599 |
return; |
| 600 |
} |
| 601 |
|
| 602 |
KGuiItem::assign(button, item); |
| 603 |
} |
| 604 |
|
| 605 |
void KoDialog::setButtonText(ButtonCode id, const QString &text) |
| 606 |
{ |
| 607 |
Q_D(KoDialog); |
| 608 |
if (!d->mSettingDetails && (id == Details)) { |
| 609 |
d->mDetailsButtonText = text; |
| 610 |
setDetailsWidgetVisible(d->mDetailsVisible); |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
QPushButton *button = this->button(id); |
| 615 |
if (button) { |
| 616 |
button->setText(text); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
QString KoDialog::buttonText(ButtonCode id) const |
| 621 |
{ |
| 622 |
QPushButton *button = this->button(id); |
| 623 |
if (button) { |
| 624 |
return button->text(); |
| 625 |
} else { |
| 626 |
return QString(); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void KoDialog::setButtonIcon(ButtonCode id, const QIcon &icon) |
| 631 |
{ |
| 632 |
QPushButton *button = this->button(id); |
| 633 |
if (button) { |
| 634 |
button->setIcon(icon); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
QIcon KoDialog::buttonIcon(ButtonCode id) const |
| 639 |
{ |
| 640 |
QPushButton *button = this->button(id); |
| 641 |
if (button) { |
| 642 |
return button->icon(); |
| 643 |
} else { |
| 644 |
return QIcon(); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
void KoDialog::setButtonToolTip(ButtonCode id, const QString &text) |
| 649 |
{ |
| 650 |
QPushButton *button = this->button(id); |
| 651 |
if (button) { |
| 652 |
if (text.isEmpty()) { |
| 653 |
button->setToolTip(QString()); |
| 654 |
} else { |
| 655 |
button->setToolTip(text); |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
QString KoDialog::buttonToolTip(ButtonCode id) const |
| 661 |
{ |
| 662 |
QPushButton *button = this->button(id); |
| 663 |
if (button) { |
| 664 |
return button->toolTip(); |
| 665 |
} else { |
| 666 |
return QString(); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
void KoDialog::setButtonWhatsThis(ButtonCode id, const QString &text) |
| 671 |
{ |
| 672 |
QPushButton *button = this->button(id); |
| 673 |
if (button) { |
| 674 |
if (text.isEmpty()) { |
| 675 |
button->setWhatsThis(QString()); |
| 676 |
} else { |
| 677 |
button->setWhatsThis(text); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
QString KoDialog::buttonWhatsThis(ButtonCode id) const |
| 683 |
{ |
| 684 |
QPushButton *button = this->button(id); |
| 685 |
if (button) { |
| 686 |
return button->whatsThis(); |
| 687 |
} else { |
| 688 |
return QString(); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
void KoDialog::setButtonFocus(ButtonCode id) |
| 693 |
{ |
| 694 |
QPushButton *button = this->button(id); |
| 695 |
if (button) { |
| 696 |
button->setFocus(); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
void KoDialog::setDetailsWidget(QWidget *detailsWidget) |
| 701 |
{ |
| 702 |
Q_D(KoDialog); |
| 703 |
if (d->mDetailsWidget == detailsWidget) { |
| 704 |
return; |
| 705 |
} |
| 706 |
delete d->mDetailsWidget; |
| 707 |
d->mDetailsWidget = detailsWidget; |
| 708 |
|
| 709 |
if (d->mDetailsWidget->parentWidget() != this) { |
| 710 |
d->mDetailsWidget->setParent(this); |
| 711 |
} |
| 712 |
|
| 713 |
d->mDetailsWidget->hide(); |
| 714 |
d->setupLayout(); |
| 715 |
|
| 716 |
if (!d->mSettingDetails) { |
| 717 |
setDetailsWidgetVisible(d->mDetailsVisible); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
bool KoDialog::isDetailsWidgetVisible() const |
| 722 |
{ |
| 723 |
return d_func()->mDetailsVisible; |
| 724 |
} |
| 725 |
|
| 726 |
void KoDialog::setDetailsWidgetVisible(bool visible) |
| 727 |
{ |
| 728 |
Q_D(KoDialog); |
| 729 |
if (d->mDetailsButtonText.isEmpty()) { |
| 730 |
d->mDetailsButtonText = i18n("&Details"); |
| 731 |
} |
| 732 |
|
| 733 |
d->mSettingDetails = true; |
| 734 |
d->mDetailsVisible = visible; |
| 735 |
if (d->mDetailsVisible) { |
| 736 |
Q_EMIT aboutToShowDetails(); |
| 737 |
setButtonText(Details, d->mDetailsButtonText + QStringLiteral(" <<")); |
| 738 |
if (d->mDetailsWidget) { |
| 739 |
if (layout()) { |
| 740 |
layout()->setEnabled(false); |
| 741 |
} |
| 742 |
|
| 743 |
d->mDetailsWidget->show(); |
| 744 |
|
| 745 |
adjustSize(); |
| 746 |
|
| 747 |
if (layout()) { |
| 748 |
layout()->activate(); |
| 749 |
layout()->setEnabled(true); |
| 750 |
} |
| 751 |
} |
| 752 |
} else { |
| 753 |
setButtonText(Details, d->mDetailsButtonText + QStringLiteral(" >>")); |
| 754 |
if (d->mDetailsWidget) { |
| 755 |
d->mDetailsWidget->hide(); |
| 756 |
} |
| 757 |
|
| 758 |
if (layout()) { |
| 759 |
layout()->activate(); |
| 760 |
adjustSize(); |
| 761 |
} |
| 762 |
|
| 763 |
} |
| 764 |
|
| 765 |
d->mSettingDetails = false; |
| 766 |
} |
| 767 |
|
| 768 |
void KoDialog::delayedDestruct() |
| 769 |
{ |
| 770 |
if (isVisible()) { |
| 771 |
hide(); |
| 772 |
} |
| 773 |
|
| 774 |
deleteLater(); |
| 775 |
} |
| 776 |
|
| 777 |
void KoDialog::slotButtonClicked(int button) |
| 778 |
{ |
| 779 |
Q_D(KoDialog); |
| 780 |
Q_EMIT buttonClicked(static_cast<KoDialog::ButtonCode>(button)); |
| 781 |
|
| 782 |
switch (button) { |
| 783 |
case Ok: |
| 784 |
Q_EMIT okClicked(); |
| 785 |
accept(); |
| 786 |
break; |
| 787 |
case Apply: |
| 788 |
Q_EMIT applyClicked(); |
| 789 |
break; |
| 790 |
case Try: |
| 791 |
Q_EMIT tryClicked(); |
| 792 |
break; |
| 793 |
case User3: |
| 794 |
Q_EMIT user3Clicked(); |
| 795 |
break; |
| 796 |
case User2: |
| 797 |
Q_EMIT user2Clicked(); |
| 798 |
break; |
| 799 |
case User1: |
| 800 |
Q_EMIT user1Clicked(); |
| 801 |
break; |
| 802 |
case Cancel: |
| 803 |
Q_EMIT cancelClicked(); |
| 804 |
reject(); |
| 805 |
break; |
| 806 |
case Close: |
| 807 |
Q_EMIT closeClicked(); |
| 808 |
done(Close); // KDE5: call reject() instead; more QDialog-like. |
| 809 |
break; |
| 810 |
case Help: |
| 811 |
Q_EMIT helpClicked(); |
| 812 |
if (!d->mAnchor.isEmpty() || !d->mHelpApp.isEmpty()) { |
| 813 |
KHelpClient::invokeHelp(d->mAnchor, d->mHelpApp); |
| 814 |
} |
| 815 |
break; |
| 816 |
case Default: |
| 817 |
Q_EMIT defaultClicked(); |
| 818 |
break; |
| 819 |
case Reset: |
| 820 |
Q_EMIT resetClicked(); |
| 821 |
break; |
| 822 |
case Details: |
| 823 |
setDetailsWidgetVisible(!d->mDetailsVisible); |
| 824 |
break; |
| 825 |
} |
| 826 |
|
| 827 |
// If we're here from the closeEvent, and auto-delete is on, well, auto-delete now. |
| 828 |
if (d->mDeferredDelete) { |
| 829 |
d->mDeferredDelete = false; |
| 830 |
delayedDestruct(); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
void KoDialog::enableLinkedHelp(bool state) |
| 835 |
{ |
| 836 |
Q_D(KoDialog); |
| 837 |
if ((d->mUrlHelp != nullptr) == state) { |
| 838 |
return; |
| 839 |
} |
| 840 |
if (state) { |
| 841 |
if (d->mUrlHelp) { |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
d->mUrlHelp = new KUrlLabel(this); |
| 846 |
d->mUrlHelp->setText(helpLinkText()); |
| 847 |
d->mUrlHelp->setFloatEnabled(true); |
| 848 |
d->mUrlHelp->setUnderline(true); |
| 849 |
d->mUrlHelp->setMinimumHeight(fontMetrics().height() + QApplication::style()->pixelMetric(QStyle::PM_LayoutTopMargin)); |
| 850 |
connect(d->mUrlHelp, SIGNAL(leftClickedUrl()), SLOT(helpLinkClicked())); |
| 851 |
|
| 852 |
d->mUrlHelp->show(); |
| 853 |
} else { |
| 854 |
delete d->mUrlHelp; |
| 855 |
d->mUrlHelp = nullptr; |
| 856 |
} |
| 857 |
|
| 858 |
d->setupLayout(); |
| 859 |
} |
| 860 |
|
| 861 |
void KoDialog::setHelp(const QString &anchor, const QString &appname) |
| 862 |
{ |
| 863 |
Q_D(KoDialog); |
| 864 |
d->mAnchor = anchor; |
| 865 |
d->mHelpApp = appname; |
| 866 |
} |
| 867 |
|
| 868 |
void KoDialog::setHelpLinkText(const QString &text) |
| 869 |
{ |
| 870 |
Q_D(KoDialog); |
| 871 |
d->mHelpLinkText = text; |
| 872 |
if (d->mUrlHelp) { |
| 873 |
d->mUrlHelp->setText(helpLinkText()); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
QString KoDialog::helpLinkText() const |
| 878 |
{ |
| 879 |
Q_D(const KoDialog); |
| 880 |
return (d->mHelpLinkText.isEmpty() ? i18n("Get help...") : d->mHelpLinkText); |
| 881 |
} |
| 882 |
|
| 883 |
void KoDialog::updateGeometry() |
| 884 |
{ |
| 885 |
} |
| 886 |
|
| 887 |
void KoDialog::hideEvent(QHideEvent *event) |
| 888 |
{ |
| 889 |
Q_UNUSED(event) |
| 890 |
Q_EMIT hidden(); |
| 891 |
} |
| 892 |
|
| 893 |
void KoDialog::closeEvent(QCloseEvent *event) |
| 894 |
{ |
| 895 |
Q_D(KoDialog); |
| 896 |
QPushButton *button = this->button(d->mEscapeButton); |
| 897 |
if (button && !isHidden()) { |
| 898 |
button->animateClick(); |
| 899 |
|
| 900 |
if (testAttribute(Qt::WA_DeleteOnClose)) { |
| 901 |
// Don't let QWidget::close do a deferred delete just yet, wait for the click first |
| 902 |
d->mDeferredDelete = true; |
| 903 |
setAttribute(Qt::WA_DeleteOnClose, false); |
| 904 |
} |
| 905 |
} else { |
| 906 |
QDialog::closeEvent(event); |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
#include "moc_KoDialog.cpp" |