1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
|
/*
This file is part of the KDE libraries
SPDX-FileCopyrightText: 2000 Reginald Stadlbauer <reggie@kde.org>
SPDX-FileCopyrightText: 1997, 1998 Stephan Kulow <coolo@kde.org>
SPDX-FileCopyrightText: 1997, 1998 Mark Donohoe <donohoe@kde.org>
SPDX-FileCopyrightText: 1997, 1998 Sven Radej <radej@kde.org>
SPDX-FileCopyrightText: 1997, 1998 Matthias Ettrich <ettrich@kde.org>
SPDX-FileCopyrightText: 1999 Chris Schlaeger <cs@kde.org>
SPDX-FileCopyrightText: 1999 Kurt Granroth <granroth@kde.org>
SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include "ktoolbar.h"
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QDomElement>
#include <QDrag>
#include <QFrame>
#include <QLayout>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
#include <QPointer>
#ifdef WITH_QTDBUS
#include <QDBusConnection>
#include <QDBusMessage>
#endif
#include <KAuthorized>
#include <KConfig>
#include <KConfigGroup>
#include <KIconTheme>
#include <KLocalizedString>
#include <KSharedConfig>
#include <KStandardActions>
#include <KToggleAction>
#include <KToolBarPopupAction>
#include "kactioncollection.h"
#include "kedittoolbar.h"
#include "kxmlguifactory.h"
#include "kxmlguiwindow.h"
#include "ktoolbarhelper_p.h"
#include <algorithm>
/*
Toolbar settings (e.g. icon size or toolButtonStyle)
=====================================================
We have the following stack of settings (in order of priority) :
- user-specified settings (loaded/saved in KConfig)
- developer-specified settings in the XMLGUI file (if using xmlgui) (cannot change at runtime)
- KDE-global default (user-configurable; can change at runtime)
and when switching between kparts, they are saved as xml in memory,
which, in the unlikely case of no-kmainwindow-autosaving, could be
different from the user-specified settings saved in KConfig and would have
priority over it.
So, in summary, without XML:
Global config / User settings (loaded/saved in kconfig)
and with XML:
Global config / App-XML attributes / User settings (loaded/saved in kconfig)
And all those settings (except the KDE-global defaults) have to be stored in memory
since we cannot retrieve them at random points in time, not knowing the xml document
nor config file that holds these settings. Hence the iconSizeSettings and toolButtonStyleSettings arrays.
For instance, if you change the KDE-global default, whether this makes a change
on a given toolbar depends on whether there are settings at Level_AppXML or Level_UserSettings.
Only if there are no settings at those levels, should the change of KDEDefault make a difference.
*/
enum SettingLevel { Level_KDEDefault, Level_AppXML, Level_UserSettings, NSettingLevels };
enum { Unset = -1 };
class KToolBarPrivate
{
public:
KToolBarPrivate(KToolBar *qq)
: q(qq)
, isMainToolBar(false)
, unlockedMovable(true)
, contextOrient(nullptr)
, contextMode(nullptr)
, contextSize(nullptr)
, contextButtonTitle(nullptr)
, contextShowText(nullptr)
, contextButtonAction(nullptr)
, contextTop(nullptr)
, contextLeft(nullptr)
, contextRight(nullptr)
, contextBottom(nullptr)
, contextIcons(nullptr)
, contextTextRight(nullptr)
, contextText(nullptr)
, contextTextUnder(nullptr)
, contextLockAction(nullptr)
, dropIndicatorAction(nullptr)
, context(nullptr)
, dragAction(nullptr)
{
}
void slotAppearanceChanged();
void slotContextAboutToShow();
void slotContextAboutToHide();
void slotContextLeft();
void slotContextRight();
void slotContextShowText();
void slotContextTop();
void slotContextBottom();
void slotContextIcons();
void slotContextText();
void slotContextTextRight();
void slotContextTextUnder();
void slotContextIconSize(QAction *action);
void slotLockToolBars(bool lock);
void init(bool readConfig = true, bool isMainToolBar = false);
QString getPositionAsString() const;
QMenu *contextMenu(const QPoint &globalPos);
void setLocked(bool locked);
void adjustSeparatorVisibility();
void loadKDESettings();
void applyCurrentSettings();
QAction *findAction(const QString &actionName, KXMLGUIClient **client = nullptr) const;
static Qt::ToolButtonStyle toolButtonStyleFromString(const QString &style);
static QString toolButtonStyleToString(Qt::ToolButtonStyle);
static Qt::ToolBarArea positionFromString(const QString &position);
static Qt::ToolButtonStyle toolButtonStyleSetting();
KToolBar *const q;
bool isMainToolBar : 1;
bool unlockedMovable : 1;
static bool s_editable;
static bool s_locked;
QSet<KXMLGUIClient *> xmlguiClients;
QMenu *contextOrient;
QMenu *contextMode;
QMenu *contextSize;
QAction *contextButtonTitle;
QAction *contextShowText;
QAction *contextButtonAction;
QAction *contextTop;
QAction *contextLeft;
QAction *contextRight;
QAction *contextBottom;
QAction *contextIcons;
QAction *contextTextRight;
QAction *contextText;
QAction *contextTextUnder;
KToggleAction *contextLockAction;
struct ContextIconInfo {
QAction *iconAction = nullptr;
int iconSize = 0;
};
std::vector<ContextIconInfo> m_contextIconSizes;
class IntSetting
{
public:
IntSetting()
{
for (int &value : values) {
value = Unset;
}
}
int currentValue() const
{
int val = Unset;
for (int value : values) {
if (value != Unset) {
val = value;
}
}
return val;
}
// Default value as far as the user is concerned is kde-global + app-xml.
// If currentValue()==defaultValue() then nothing to write into kconfig.
int defaultValue() const
{
int val = Unset;
for (int level = 0; level < Level_UserSettings; ++level) {
if (values[level] != Unset) {
val = values[level];
}
}
return val;
}
QString toString() const
{
QString str;
for (int value : values) {
str += QString::number(value) + QLatin1Char(' ');
}
return str;
}
int &operator[](int index)
{
return values[index];
}
private:
int values[NSettingLevels];
};
IntSetting iconSizeSettings;
IntSetting toolButtonStyleSettings; // either Qt::ToolButtonStyle or -1, hence "int".
QList<QAction *> actionsBeingDragged;
QAction *dropIndicatorAction;
QMenu *context;
QAction *dragAction;
QPoint dragStartPosition;
};
bool KToolBarPrivate::s_editable = false;
bool KToolBarPrivate::s_locked = true;
void KToolBarPrivate::init(bool readConfig, bool _isMainToolBar)
{
isMainToolBar = _isMainToolBar;
loadKDESettings();
// also read in our configurable settings (for non-xmlgui toolbars)
if (readConfig) {
KConfigGroup cg(KSharedConfig::openConfig(), QString());
q->applySettings(cg);
}
if (q->mainWindow()) {
// Get notified when settings change
QObject::connect(q, &QToolBar::allowedAreasChanged, q->mainWindow(), &KMainWindow::setSettingsDirty);
QObject::connect(q, &QToolBar::iconSizeChanged, q->mainWindow(), &KMainWindow::setSettingsDirty);
QObject::connect(q, &QToolBar::toolButtonStyleChanged, q->mainWindow(), &KMainWindow::setSettingsDirty);
QObject::connect(q, &QToolBar::movableChanged, q->mainWindow(), &KMainWindow::setSettingsDirty);
QObject::connect(q, &QToolBar::orientationChanged, q->mainWindow(), &KMainWindow::setSettingsDirty);
}
if (!KAuthorized::authorize(QStringLiteral("movable_toolbars"))) {
q->setMovable(false);
} else {
q->setMovable(!KToolBar::toolBarsLocked());
}
q->toggleViewAction()->setEnabled(KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar")));
QObject::connect(q, &QToolBar::movableChanged, q, &KToolBar::slotMovableChanged);
q->setAcceptDrops(true);
#ifdef WITH_QTDBUS
QDBusConnection::sessionBus()
.connect(QString(), QStringLiteral("/KToolBar"), QStringLiteral("org.kde.KToolBar"), QStringLiteral("styleChanged"), q, SLOT(slotAppearanceChanged()));
#endif
QObject::connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, q, [this]() {
slotAppearanceChanged();
});
}
QString KToolBarPrivate::getPositionAsString() const
{
if (!q->mainWindow()) {
return QStringLiteral("None");
}
// get all of the stuff to save
switch (q->mainWindow()->toolBarArea(const_cast<KToolBar *>(q))) {
case Qt::BottomToolBarArea:
return QStringLiteral("Bottom");
case Qt::LeftToolBarArea:
return QStringLiteral("Left");
case Qt::RightToolBarArea:
return QStringLiteral("Right");
case Qt::TopToolBarArea:
default:
return QStringLiteral("Top");
}
}
QMenu *KToolBarPrivate::contextMenu(const QPoint &globalPos)
{
if (!context) {
context = new QMenu(q);
context->setIcon(QIcon::fromTheme(QStringLiteral("configure-toolbars")));
context->setTitle(i18nc("@title:menu", "Toolbar Settings"));
contextButtonTitle = context->addSection(i18nc("@title:menu", "Show Text"));
contextShowText = context->addAction(QString(), q, [this]() {
slotContextShowText();
});
context->addSection(i18nc("@title:menu", "Toolbar Settings"));
contextOrient = new QMenu(i18nc("Toolbar orientation", "Orientation"), context);
contextTop = contextOrient->addAction(i18nc("toolbar position string", "Top"), q, [this]() {
slotContextTop();
});
contextTop->setChecked(true);
contextLeft = contextOrient->addAction(i18nc("toolbar position string", "Left"), q, [this]() {
slotContextLeft();
});
contextRight = contextOrient->addAction(i18nc("toolbar position string", "Right"), q, [this]() {
slotContextRight();
});
contextBottom = contextOrient->addAction(i18nc("toolbar position string", "Bottom"), q, [this]() {
slotContextBottom();
});
QActionGroup *positionGroup = new QActionGroup(contextOrient);
const auto orientActions = contextOrient->actions();
for (QAction *action : orientActions) {
action->setActionGroup(positionGroup);
action->setCheckable(true);
}
contextMode = new QMenu(i18n("Text Position"), context);
contextIcons = contextMode->addAction(i18nc("@item:inmenu", "Icons Only"), q, [this]() {
slotContextIcons();
});
contextText = contextMode->addAction(i18nc("@item:inmenu", "Text Only"), q, [this]() {
slotContextText();
});
contextTextRight = contextMode->addAction(i18nc("@item:inmenu", "Text Alongside Icons"), q, [this]() {
slotContextTextRight();
});
contextTextUnder = contextMode->addAction(i18nc("@item:inmenu", "Text Under Icons"), q, [this]() {
slotContextTextUnder();
});
QActionGroup *textGroup = new QActionGroup(contextMode);
const auto modeActions = contextMode->actions();
for (QAction *action : modeActions) {
action->setActionGroup(textGroup);
action->setCheckable(true);
}
contextSize = new QMenu(i18n("Icon Size"), context);
auto *act = contextSize->addAction(i18nc("@item:inmenu Icon size", "Default"));
q->connect(act, &QAction::triggered, q, [this, act]() {
slotContextIconSize(act);
});
m_contextIconSizes.push_back({act, iconSizeSettings.defaultValue()});
// Query the current theme for available sizes
KIconTheme *theme = KIconLoader::global()->theme();
QList<int> avSizes;
if (theme) {
avSizes = theme->querySizes(isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
}
std::sort(avSizes.begin(), avSizes.end());
if (avSizes.count() < 10) {
// Fixed or threshold type icons
for (int it : std::as_const(avSizes)) {
QString text;
if (it < 19) {
text = i18n("Small (%1x%2)", it, it);
} else if (it < 25) {
text = i18n("Medium (%1x%2)", it, it);
} else if (it < 35) {
text = i18n("Large (%1x%2)", it, it);
} else {
text = i18n("Huge (%1x%2)", it, it);
}
auto *act = contextSize->addAction(text);
q->connect(act, &QAction::triggered, q, [this, act]() {
slotContextIconSize(act);
});
m_contextIconSizes.push_back({act, it});
}
} else {
// Scalable icons.
const int progression[] = {16, 22, 32, 48, 64, 96, 128, 192, 256};
for (int p : progression) {
for (int it : std::as_const(avSizes)) {
if (it >= p) {
QString text;
if (it < 19) {
text = i18n("Small (%1x%2)", it, it);
} else if (it < 25) {
text = i18n("Medium (%1x%2)", it, it);
} else if (it < 35) {
text = i18n("Large (%1x%2)", it, it);
} else {
text = i18n("Huge (%1x%2)", it, it);
}
auto *act = contextSize->addAction(text);
q->connect(act, &QAction::triggered, q, [this, act]() {
slotContextIconSize(act);
});
m_contextIconSizes.push_back({act, it});
break;
}
}
}
}
QActionGroup *sizeGroup = new QActionGroup(contextSize);
const auto sizeActions = contextSize->actions();
for (QAction *action : sizeActions) {
action->setActionGroup(sizeGroup);
action->setCheckable(true);
}
if (!q->toolBarsLocked() && !q->isMovable()) {
unlockedMovable = false;
}
delete contextLockAction;
contextLockAction = new KToggleAction(QIcon::fromTheme(QStringLiteral("system-lock-screen")), i18n("Lock Toolbar Positions"), q);
contextLockAction->setChecked(q->toolBarsLocked());
QObject::connect(contextLockAction, &KToggleAction::toggled, q, [this](bool checked) {
slotLockToolBars(checked);
});
// Now add the actions to the menu
context->addMenu(contextMode);
context->addMenu(contextSize);
context->addMenu(contextOrient);
context->addSeparator();
QObject::connect(context, &QMenu::aboutToShow, q, [this]() {
slotContextAboutToShow();
});
}
contextButtonAction = q->actionAt(q->mapFromGlobal(globalPos));
if (contextButtonAction) {
contextShowText->setText(contextButtonAction->text());
contextShowText->setIcon(contextButtonAction->icon());
contextShowText->setCheckable(true);
}
contextOrient->menuAction()->setVisible(!q->toolBarsLocked());
// Unplugging a submenu from abouttohide leads to the popupmenu floating around
// So better simply call that code from after exec() returns (DF)
// connect(context, SIGNAL(aboutToHide()), this, SLOT(slotContextAboutToHide()));
// For tool buttons with delay popup or menu button popup (split button)
// show the actions of that button for ease of access.
// (no need to wait for the menu to open or aim at the arrow)
if (auto *contextToolButton = qobject_cast<QToolButton *>(q->widgetForAction(contextButtonAction))) {
if (contextToolButton->popupMode() == QToolButton::DelayedPopup || contextToolButton->popupMode() == QToolButton::MenuButtonPopup) {
auto *actionMenu = contextButtonAction->menu();
if (!actionMenu) {
if (auto *toolBarPopupAction = qobject_cast<KToolBarPopupAction *>(contextButtonAction)) {
actionMenu = toolBarPopupAction->popupMenu();
}
}
if (actionMenu) {
// In case it is populated on demand
Q_EMIT actionMenu->aboutToShow();
auto *contextMenu = new QMenu(q);
contextMenu->setAttribute(Qt::WA_DeleteOnClose);
const auto actions = actionMenu->actions();
if (!actions.isEmpty()) {
for (QAction *action : actions) {
contextMenu->addAction(action);
}
// Now add the configure actions as submenu
contextMenu->addSeparator();
contextMenu->addMenu(context);
return contextMenu;
}
}
}
}
return context;
}
void KToolBarPrivate::setLocked(bool locked)
{
if (unlockedMovable) {
q->setMovable(!locked);
}
}
void KToolBarPrivate::adjustSeparatorVisibility()
{
bool visibleNonSeparator = false;
int separatorToShow = -1;
for (int index = 0; index < q->actions().count(); ++index) {
QAction *action = q->actions().at(index);
if (action->isSeparator()) {
if (visibleNonSeparator) {
separatorToShow = index;
visibleNonSeparator = false;
} else {
action->setVisible(false);
}
} else if (!visibleNonSeparator) {
if (action->isVisible()) {
visibleNonSeparator = true;
if (separatorToShow != -1) {
q->actions().at(separatorToShow)->setVisible(true);
separatorToShow = -1;
}
}
}
}
if (separatorToShow != -1) {
q->actions().at(separatorToShow)->setVisible(false);
}
}
Qt::ToolButtonStyle KToolBarPrivate::toolButtonStyleFromString(const QString &_style)
{
QString style = _style.toLower();
if (style == QLatin1String("textbesideicon") || style == QLatin1String("icontextright")) {
return Qt::ToolButtonTextBesideIcon;
} else if (style == QLatin1String("textundericon") || style == QLatin1String("icontextbottom")) {
return Qt::ToolButtonTextUnderIcon;
} else if (style == QLatin1String("textonly")) {
return Qt::ToolButtonTextOnly;
} else {
return Qt::ToolButtonIconOnly;
}
}
QString KToolBarPrivate::toolButtonStyleToString(Qt::ToolButtonStyle style)
{
switch (style) {
case Qt::ToolButtonIconOnly:
default:
return QStringLiteral("IconOnly");
case Qt::ToolButtonTextBesideIcon:
return QStringLiteral("TextBesideIcon");
case Qt::ToolButtonTextOnly:
return QStringLiteral("TextOnly");
case Qt::ToolButtonTextUnderIcon:
return QStringLiteral("TextUnderIcon");
}
}
Qt::ToolBarArea KToolBarPrivate::positionFromString(const QString &position)
{
Qt::ToolBarArea newposition = Qt::TopToolBarArea;
if (position == QLatin1String("left")) {
newposition = Qt::LeftToolBarArea;
} else if (position == QLatin1String("bottom")) {
newposition = Qt::BottomToolBarArea;
} else if (position == QLatin1String("right")) {
newposition = Qt::RightToolBarArea;
} else if (position == QLatin1String("none")) {
newposition = Qt::NoToolBarArea;
}
return newposition;
}
// Global setting was changed
void KToolBarPrivate::slotAppearanceChanged()
{
loadKDESettings();
applyCurrentSettings();
}
Qt::ToolButtonStyle KToolBarPrivate::toolButtonStyleSetting()
{
KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("Toolbar style"));
const QString fallback = KToolBarPrivate::toolButtonStyleToString(Qt::ToolButtonTextBesideIcon);
return KToolBarPrivate::toolButtonStyleFromString(group.readEntry("ToolButtonStyle", fallback));
}
void KToolBarPrivate::loadKDESettings()
{
iconSizeSettings[Level_KDEDefault] = q->iconSizeDefault();
if (isMainToolBar) {
toolButtonStyleSettings[Level_KDEDefault] = toolButtonStyleSetting();
} else {
const QString fallBack = toolButtonStyleToString(Qt::ToolButtonTextBesideIcon);
/**
TODO: if we get complaints about text beside icons on small screens,
try the following code out on such systems - aseigo.
// if we are on a small screen with a non-landscape ratio, then
// we revert to text under icons since width is probably not our
// friend in such cases
QDesktopWidget *desktop = QApplication::desktop();
QRect screenGeom = desktop->screenGeometry(desktop->primaryScreen());
qreal ratio = screenGeom.width() / qreal(screenGeom.height());
if (screenGeom.width() < 1024 && ratio <= 1.4) {
fallBack = "TextUnderIcon";
}
**/
KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("Toolbar style"));
const QString value = group.readEntry("ToolButtonStyleOtherToolbars", fallBack);
toolButtonStyleSettings[Level_KDEDefault] = KToolBarPrivate::toolButtonStyleFromString(value);
}
}
// Call this after changing something in d->iconSizeSettings or d->toolButtonStyleSettings
void KToolBarPrivate::applyCurrentSettings()
{
// qCDebug(DEBUG_KXMLGUI) << q->objectName() << "iconSizeSettings:" << iconSizeSettings.toString() << "->" << iconSizeSettings.currentValue();
const int currentIconSize = iconSizeSettings.currentValue();
q->setIconSize(QSize(currentIconSize, currentIconSize));
// qCDebug(DEBUG_KXMLGUI) << q->objectName() << "toolButtonStyleSettings:" << toolButtonStyleSettings.toString() << "->" <<
// toolButtonStyleSettings.currentValue();
q->setToolButtonStyle(static_cast<Qt::ToolButtonStyle>(toolButtonStyleSettings.currentValue()));
// And remember to save the new look later
KMainWindow *kmw = q->mainWindow();
if (kmw) {
kmw->setSettingsDirty();
}
}
QAction *KToolBarPrivate::findAction(const QString &actionName, KXMLGUIClient **clientOut) const
{
for (KXMLGUIClient *client : xmlguiClients) {
QAction *action = client->actionCollection()->action(actionName);
if (action) {
if (clientOut) {
*clientOut = client;
}
return action;
}
}
return nullptr;
}
void KToolBarPrivate::slotContextAboutToShow()
{
/**
* The idea here is to reuse the "static" part of the menu to save time.
* But the "Toolbars" action is dynamic (can be a single action or a submenu)
* and ToolBarHandler::setupActions() deletes it, so better not keep it around.
* So we currently plug/unplug the last two actions of the menu.
* Another way would be to keep around the actions and plug them all into a (new each time) popupmenu.
*/
KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
// try to find "configure toolbars" action
QAction *configureAction = nullptr;
const QString actionName = KStandardActions::name(KStandardActions::ConfigureToolbars);
configureAction = findAction(actionName);
if (!configureAction && kmw) {
configureAction = kmw->actionCollection()->action(actionName);
}
if (configureAction) {
context->addAction(configureAction);
}
context->addAction(contextLockAction);
if (kmw) {
kmw->setupToolbarMenuActions();
// Only allow hiding a toolbar if the action is also plugged somewhere else (e.g. menubar)
QAction *tbAction = kmw->toolBarMenuAction();
if (!q->toolBarsLocked() && tbAction) {
const QList<QObject *> associatedObjects = tbAction->associatedObjects();
const bool hasAssociatedWidgets = std::any_of(associatedObjects.cbegin(), associatedObjects.cend(), [](QObject *object) {
return (qobject_cast<QWidget *>(object) != nullptr);
});
if (hasAssociatedWidgets) {
context->addAction(tbAction);
}
}
}
KEditToolBar::setGlobalDefaultToolBar(q->QObject::objectName());
// Check the actions that should be checked
switch (q->toolButtonStyle()) {
case Qt::ToolButtonIconOnly:
default:
contextIcons->setChecked(true);
break;
case Qt::ToolButtonTextBesideIcon:
contextTextRight->setChecked(true);
break;
case Qt::ToolButtonTextOnly:
contextText->setChecked(true);
break;
case Qt::ToolButtonTextUnderIcon:
contextTextUnder->setChecked(true);
break;
}
auto it = std::find_if(m_contextIconSizes.cbegin(), m_contextIconSizes.cend(), [this](const ContextIconInfo &info) {
return info.iconSize == q->iconSize().width();
});
if (it != m_contextIconSizes.cend()) {
it->iconAction->setChecked(true);
}
switch (q->mainWindow()->toolBarArea(q)) {
case Qt::BottomToolBarArea:
contextBottom->setChecked(true);
break;
case Qt::LeftToolBarArea:
contextLeft->setChecked(true);
break;
case Qt::RightToolBarArea:
contextRight->setChecked(true);
break;
default:
case Qt::TopToolBarArea:
contextTop->setChecked(true);
break;
}
const bool showButtonSettings = contextButtonAction //
&& !contextShowText->text().isEmpty() //
&& contextTextRight->isChecked();
contextButtonTitle->setVisible(showButtonSettings);
contextShowText->setVisible(showButtonSettings);
if (showButtonSettings) {
contextShowText->setChecked(contextButtonAction->priority() >= QAction::NormalPriority);
}
}
void KToolBarPrivate::slotContextAboutToHide()
{
// We have to unplug whatever slotContextAboutToShow plugged into the menu.
// Unplug the toolbar menu action
KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
if (kmw) {
QAction *tbAction = kmw->toolBarMenuAction();
const QList<QObject *> associatedObjects = tbAction->associatedObjects();
const int associatedWidgetsCount = std::count_if(associatedObjects.cbegin(), associatedObjects.cend(), [](QObject *object) {
return (qobject_cast<QWidget *>(object) != nullptr);
});
if (associatedWidgetsCount > 1) {
context->removeAction(tbAction);
}
}
// Unplug the configure toolbars action too, since it's afterwards anyway
QAction *configureAction = nullptr;
const QString actionName = KStandardActions::name(KStandardActions::ConfigureToolbars);
configureAction = findAction(actionName);
if (!configureAction && kmw) {
configureAction = kmw->actionCollection()->action(actionName);
}
if (configureAction) {
context->removeAction(configureAction);
}
context->removeAction(contextLockAction);
}
void KToolBarPrivate::slotContextLeft()
{
q->mainWindow()->addToolBar(Qt::LeftToolBarArea, q);
}
void KToolBarPrivate::slotContextRight()
{
q->mainWindow()->addToolBar(Qt::RightToolBarArea, q);
}
void KToolBarPrivate::slotContextShowText()
{
Q_ASSERT(contextButtonAction);
const QAction::Priority priority = contextShowText->isChecked() ? QAction::NormalPriority : QAction::LowPriority;
contextButtonAction->setPriority(priority);
// Find to which xml file and componentData the action belongs to
QString componentName;
QString filename;
KXMLGUIClient *client;
if (findAction(contextButtonAction->objectName(), &client)) {
componentName = client->componentName();
filename = client->xmlFile();
}
if (filename.isEmpty()) {
componentName = QCoreApplication::applicationName();
filename = componentName + QLatin1String("ui.rc");
}
// Save the priority state of the action
const QString configFile = KXMLGUIFactory::readConfigFile(filename, componentName);
QDomDocument document;
document.setContent(configFile);
QDomElement elem = KXMLGUIFactory::actionPropertiesElement(document);
QDomElement actionElem = KXMLGUIFactory::findActionByName(elem, contextButtonAction->objectName(), true);
actionElem.setAttribute(QStringLiteral("priority"), priority);
KXMLGUIFactory::saveConfigFile(document, filename, componentName);
}
void KToolBarPrivate::slotContextTop()
{
q->mainWindow()->addToolBar(Qt::TopToolBarArea, q);
}
void KToolBarPrivate::slotContextBottom()
{
q->mainWindow()->addToolBar(Qt::BottomToolBarArea, q);
}
void KToolBarPrivate::slotContextIcons()
{
q->setToolButtonStyle(Qt::ToolButtonIconOnly);
toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
}
void KToolBarPrivate::slotContextText()
{
q->setToolButtonStyle(Qt::ToolButtonTextOnly);
toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
}
void KToolBarPrivate::slotContextTextUnder()
{
q->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
}
void KToolBarPrivate::slotContextTextRight()
{
q->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
}
void KToolBarPrivate::slotContextIconSize(QAction *action)
{
if (action) {
auto it = std::find_if(m_contextIconSizes.cbegin(), m_contextIconSizes.cend(), [action](const ContextIconInfo &info) {
return info.iconAction == action;
});
if (it != m_contextIconSizes.cend()) {
q->setIconDimensions(it->iconSize);
}
}
}
void KToolBarPrivate::slotLockToolBars(bool lock)
{
q->setToolBarsLocked(lock);
}
KToolBar::KToolBar(QWidget *parent, bool isMainToolBar, bool readConfig)
: QToolBar(parent)
, d(new KToolBarPrivate(this))
{
d->init(readConfig, isMainToolBar);
// KToolBar is auto-added to the top area of the main window if parent is a QMainWindow
if (QMainWindow *mw = qobject_cast<QMainWindow *>(parent)) {
mw->addToolBar(this);
}
}
KToolBar::KToolBar(const QString &objectName, QWidget *parent, bool readConfig)
: QToolBar(parent)
, d(new KToolBarPrivate(this))
{
setObjectName(objectName);
// mainToolBar -> isMainToolBar = true -> buttonStyle is configurable
// others -> isMainToolBar = false -> ### hardcoded default for buttonStyle !!! should be configurable? -> hidden key added
d->init(readConfig, (objectName == QLatin1String("mainToolBar")));
// KToolBar is auto-added to the top area of the main window if parent is a QMainWindow
if (QMainWindow *mw = qobject_cast<QMainWindow *>(parent)) {
mw->addToolBar(this);
}
}
KToolBar::KToolBar(const QString &objectName, QMainWindow *parent, Qt::ToolBarArea area, bool newLine, bool isMainToolBar, bool readConfig)
: QToolBar(parent)
, d(new KToolBarPrivate(this))
{
setObjectName(objectName);
d->init(readConfig, isMainToolBar);
if (newLine) {
mainWindow()->addToolBarBreak(area);
}
mainWindow()->addToolBar(area, this);
if (newLine) {
mainWindow()->addToolBarBreak(area);
}
}
KToolBar::~KToolBar()
{
delete d->contextLockAction;
}
void KToolBar::saveSettings(KConfigGroup &cg)
{
Q_ASSERT(!cg.name().isEmpty());
const int currentIconSize = iconSize().width();
// qCDebug(DEBUG_KXMLGUI) << objectName() << currentIconSize << d->iconSizeSettings.toString() << "defaultValue=" << d->iconSizeSettings.defaultValue();
if (!cg.hasDefault("IconSize") && currentIconSize == d->iconSizeSettings.defaultValue()) {
cg.revertToDefault("IconSize");
d->iconSizeSettings[Level_UserSettings] = Unset;
} else {
cg.writeEntry("IconSize", currentIconSize);
d->iconSizeSettings[Level_UserSettings] = currentIconSize;
}
const Qt::ToolButtonStyle currentToolButtonStyle = toolButtonStyle();
if (!cg.hasDefault("ToolButtonStyle") && currentToolButtonStyle == d->toolButtonStyleSettings.defaultValue()) {
cg.revertToDefault("ToolButtonStyle");
d->toolButtonStyleSettings[Level_UserSettings] = Unset;
} else {
cg.writeEntry("ToolButtonStyle", d->toolButtonStyleToString(currentToolButtonStyle));
d->toolButtonStyleSettings[Level_UserSettings] = currentToolButtonStyle;
}
}
void KToolBar::addXMLGUIClient(KXMLGUIClient *client)
{
d->xmlguiClients << client;
}
void KToolBar::removeXMLGUIClient(KXMLGUIClient *client)
{
d->xmlguiClients.remove(client);
}
void KToolBar::contextMenuEvent(QContextMenuEvent *event)
{
if (mainWindow()) {
QPointer<KToolBar> guard(this);
const QPoint globalPos = event->globalPos();
d->contextMenu(globalPos)->exec(globalPos);
// "Configure Toolbars" recreates toolbars, so we might not exist anymore.
if (guard) {
d->slotContextAboutToHide();
}
return;
}
QToolBar::contextMenuEvent(event);
}
void KToolBar::loadState(const QDomElement &element)
{
QMainWindow *mw = mainWindow();
if (!mw) {
return;
}
{
const QString &i18nText = KToolbarHelper::i18nToolBarName(element);
if (!i18nText.isEmpty()) {
setWindowTitle(i18nText);
}
}
/*
This method is called in order to load toolbar settings from XML.
However this can be used in two rather different cases:
- for the initial loading of the app's XML. In that case the settings
are only the defaults (Level_AppXML), the user's KConfig settings will override them
- for later re-loading when switching between parts in KXMLGUIFactory.
In that case the XML contains the final settings, not the defaults.
We do need the defaults, and the toolbar might have been completely
deleted and recreated meanwhile. So we store the app-default settings
into the XML.
*/
bool loadingAppDefaults = true;
if (element.hasAttribute(QStringLiteral("tempXml"))) {
// this isn't the first time, so the app-xml defaults have been saved into the (in-memory) XML
loadingAppDefaults = false;
const QString iconSizeDefault = element.attribute(QStringLiteral("iconSizeDefault"));
if (!iconSizeDefault.isEmpty()) {
d->iconSizeSettings[Level_AppXML] = iconSizeDefault.toInt();
}
const QString toolButtonStyleDefault = element.attribute(QStringLiteral("toolButtonStyleDefault"));
if (!toolButtonStyleDefault.isEmpty()) {
d->toolButtonStyleSettings[Level_AppXML] = d->toolButtonStyleFromString(toolButtonStyleDefault);
}
} else {
// loading app defaults
bool newLine = false;
QString attrNewLine = element.attribute(QStringLiteral("newline")).toLower();
if (!attrNewLine.isEmpty()) {
newLine = (attrNewLine == QLatin1String("true"));
}
if (newLine && mw) {
mw->insertToolBarBreak(this);
}
}
int newIconSize = -1;
if (element.hasAttribute(QStringLiteral("iconSize"))) {
bool ok;
newIconSize = element.attribute(QStringLiteral("iconSize")).trimmed().toInt(&ok);
if (!ok) {
newIconSize = -1;
}
}
if (newIconSize != -1) {
d->iconSizeSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = newIconSize;
}
const QString newToolButtonStyle = element.attribute(QStringLiteral("iconText"));
if (!newToolButtonStyle.isEmpty()) {
d->toolButtonStyleSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = d->toolButtonStyleFromString(newToolButtonStyle);
}
bool hidden = false;
{
QString attrHidden = element.attribute(QStringLiteral("hidden")).toLower();
if (!attrHidden.isEmpty()) {
hidden = (attrHidden == QLatin1String("true"));
}
}
Qt::ToolBarArea pos = Qt::NoToolBarArea;
{
QString attrPosition = element.attribute(QStringLiteral("position")).toLower();
if (!attrPosition.isEmpty()) {
pos = KToolBarPrivate::positionFromString(attrPosition);
}
}
if (pos != Qt::NoToolBarArea) {
mw->addToolBar(pos, this);
}
setVisible(!hidden);
d->applyCurrentSettings();
}
// Called when switching between xmlgui clients, in order to find any unsaved settings
// again when switching back to the current xmlgui client.
void KToolBar::saveState(QDomElement ¤t) const
{
Q_ASSERT(!current.isNull());
current.setAttribute(QStringLiteral("tempXml"), QStringLiteral("true"));
current.setAttribute(QStringLiteral("noMerge"), QStringLiteral("1"));
current.setAttribute(QStringLiteral("position"), d->getPositionAsString().toLower());
current.setAttribute(QStringLiteral("hidden"), isHidden() ? QStringLiteral("true") : QStringLiteral("false"));
const int currentIconSize = iconSize().width();
if (currentIconSize == d->iconSizeSettings.defaultValue()) {
current.removeAttribute(QStringLiteral("iconSize"));
} else {
current.setAttribute(QStringLiteral("iconSize"), iconSize().width());
}
if (toolButtonStyle() == d->toolButtonStyleSettings.defaultValue()) {
current.removeAttribute(QStringLiteral("iconText"));
} else {
current.setAttribute(QStringLiteral("iconText"), d->toolButtonStyleToString(toolButtonStyle()));
}
// Note: if this method is used by more than KXMLGUIBuilder, e.g. to save XML settings to *disk*,
// then the stuff below shouldn't always be done. This is not the case currently though.
if (d->iconSizeSettings[Level_AppXML] != Unset) {
current.setAttribute(QStringLiteral("iconSizeDefault"), d->iconSizeSettings[Level_AppXML]);
}
if (d->toolButtonStyleSettings[Level_AppXML] != Unset) {
const Qt::ToolButtonStyle bs = static_cast<Qt::ToolButtonStyle>(d->toolButtonStyleSettings[Level_AppXML]);
current.setAttribute(QStringLiteral("toolButtonStyleDefault"), d->toolButtonStyleToString(bs));
}
}
// called by KMainWindow::applyMainWindowSettings to read from the user settings
void KToolBar::applySettings(const KConfigGroup &cg)
{
Q_ASSERT(!cg.name().isEmpty());
if (cg.hasKey("IconSize")) {
d->iconSizeSettings[Level_UserSettings] = cg.readEntry("IconSize", 0);
}
if (cg.hasKey("ToolButtonStyle")) {
d->toolButtonStyleSettings[Level_UserSettings] = d->toolButtonStyleFromString(cg.readEntry("ToolButtonStyle", QString()));
}
d->applyCurrentSettings();
}
KMainWindow *KToolBar::mainWindow() const
{
return qobject_cast<KMainWindow *>(const_cast<QObject *>(parent()));
}
void KToolBar::setIconDimensions(int size)
{
QToolBar::setIconSize(QSize(size, size));
d->iconSizeSettings[Level_UserSettings] = size;
}
int KToolBar::iconSizeDefault() const
{
return KIconLoader::global()->currentSize(d->isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
}
void KToolBar::slotMovableChanged(bool movable)
{
if (movable && !KAuthorized::authorize(QStringLiteral("movable_toolbars"))) {
setMovable(false);
}
}
void KToolBar::dragEnterEvent(QDragEnterEvent *event)
{
if (toolBarsEditable() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction)
&& event->mimeData()->hasFormat(QStringLiteral("application/x-kde-action-list"))) {
QByteArray data = event->mimeData()->data(QStringLiteral("application/x-kde-action-list"));
QDataStream stream(data);
QStringList actionNames;
stream >> actionNames;
const auto allCollections = KActionCollection::allCollections();
for (const QString &actionName : std::as_const(actionNames)) {
for (KActionCollection *ac : allCollections) {
QAction *newAction = ac->action(actionName);
if (newAction) {
d->actionsBeingDragged.append(newAction);
break;
}
}
}
if (!d->actionsBeingDragged.isEmpty()) {
QAction *overAction = actionAt(event->position().toPoint());
QFrame *dropIndicatorWidget = new QFrame(this);
dropIndicatorWidget->resize(8, height() - 4);
dropIndicatorWidget->setFrameShape(QFrame::VLine);
dropIndicatorWidget->setLineWidth(3);
d->dropIndicatorAction = insertWidget(overAction, dropIndicatorWidget);
insertAction(overAction, d->dropIndicatorAction);
event->acceptProposedAction();
return;
}
}
QToolBar::dragEnterEvent(event);
}
void KToolBar::dragMoveEvent(QDragMoveEvent *event)
{
if (toolBarsEditable()) {
Q_FOREVER {
if (d->dropIndicatorAction) {
QAction *overAction = nullptr;
const auto actions = this->actions();
for (QAction *action : actions) {
// want to make it feel that half way across an action you're dropping on the other side of it
QWidget *widget = widgetForAction(action);
if (event->position().toPoint().x() < widget->pos().x() + (widget->width() / 2)) {
overAction = action;
break;
}
}
if (overAction != d->dropIndicatorAction) {
// Check to see if the indicator is already in the right spot
int dropIndicatorIndex = actions.indexOf(d->dropIndicatorAction);
if (dropIndicatorIndex + 1 < actions.count()) {
if (actions.at(dropIndicatorIndex + 1) == overAction) {
break;
}
} else if (!overAction) {
break;
}
insertAction(overAction, d->dropIndicatorAction);
}
event->accept();
return;
}
break;
}
}
QToolBar::dragMoveEvent(event);
}
void KToolBar::dragLeaveEvent(QDragLeaveEvent *event)
{
// Want to clear this even if toolBarsEditable was changed mid-drag (unlikely)
delete d->dropIndicatorAction;
d->dropIndicatorAction = nullptr;
d->actionsBeingDragged.clear();
if (toolBarsEditable()) {
event->accept();
return;
}
QToolBar::dragLeaveEvent(event);
}
void KToolBar::dropEvent(QDropEvent *event)
{
if (toolBarsEditable()) {
for (QAction *action : std::as_const(d->actionsBeingDragged)) {
if (actions().contains(action)) {
removeAction(action);
}
insertAction(d->dropIndicatorAction, action);
}
}
// Want to clear this even if toolBarsEditable was changed mid-drag (unlikely)
delete d->dropIndicatorAction;
d->dropIndicatorAction = nullptr;
d->actionsBeingDragged.clear();
if (toolBarsEditable()) {
event->accept();
return;
}
QToolBar::dropEvent(event);
}
void KToolBar::mousePressEvent(QMouseEvent *event)
{
if (toolBarsEditable() && event->button() == Qt::LeftButton) {
if (QAction *action = actionAt(event->position().toPoint())) {
d->dragAction = action;
d->dragStartPosition = event->position().toPoint();
event->accept();
return;
}
}
QToolBar::mousePressEvent(event);
}
void KToolBar::mouseMoveEvent(QMouseEvent *event)
{
if (!toolBarsEditable() || !d->dragAction) {
QToolBar::mouseMoveEvent(event);
return;
}
if ((event->position().toPoint() - d->dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
event->accept();
return;
}
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QByteArray data;
{
QDataStream stream(&data, QIODevice::WriteOnly);
QStringList actionNames;
actionNames << d->dragAction->objectName();
stream << actionNames;
}
mimeData->setData(QStringLiteral("application/x-kde-action-list"), data);
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
if (dropAction == Qt::MoveAction) {
// Only remove from this toolbar if it was moved to another toolbar
// Otherwise the receiver moves it.
if (drag->target() != this) {
removeAction(d->dragAction);
}
}
d->dragAction = nullptr;
event->accept();
}
void KToolBar::mouseReleaseEvent(QMouseEvent *event)
{
// Want to clear this even if toolBarsEditable was changed mid-drag (unlikely)
if (d->dragAction) {
d->dragAction = nullptr;
event->accept();
return;
}
QToolBar::mouseReleaseEvent(event);
}
bool KToolBar::eventFilter(QObject *watched, QEvent *event)
{
// Generate context menu events for disabled buttons too...
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (me->buttons() & Qt::RightButton) {
if (QWidget *ww = qobject_cast<QWidget *>(watched)) {
if (ww->parent() == this && !ww->isEnabled()) {
QCoreApplication::postEvent(this,
new QContextMenuEvent(QContextMenuEvent::Mouse, me->position().toPoint(), me->globalPosition().toPoint()));
}
}
}
} else if (event->type() == QEvent::ParentChange) {
// Make sure we're not leaving stale event filters around,
// when a child is reparented somewhere else
if (QWidget *ww = qobject_cast<QWidget *>(watched)) {
if (!this->isAncestorOf(ww)) {
// New parent is not a subwidget - remove event filter
ww->removeEventFilter(this);
const auto children = ww->findChildren<QWidget *>();
for (QWidget *child : children) {
child->removeEventFilter(this);
}
}
}
}
// Redirect mouse events to the toolbar when drag + drop editing is enabled
if (toolBarsEditable()) {
if (QWidget *ww = qobject_cast<QWidget *>(watched)) {
switch (event->type()) {
case QEvent::MouseButtonPress: {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
QMouseEvent newEvent(me->type(),
mapFromGlobal(ww->mapToGlobal(me->position().toPoint())),
me->globalPosition().toPoint(),
me->button(),
me->buttons(),
me->modifiers());
mousePressEvent(&newEvent);
return true;
}
case QEvent::MouseMove: {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
QMouseEvent newEvent(me->type(),
mapFromGlobal(ww->mapToGlobal(me->position().toPoint())),
me->globalPosition().toPoint(),
me->button(),
me->buttons(),
me->modifiers());
mouseMoveEvent(&newEvent);
return true;
}
case QEvent::MouseButtonRelease: {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
QMouseEvent newEvent(me->type(),
mapFromGlobal(ww->mapToGlobal(me->position().toPoint())),
me->globalPosition().toPoint(),
me->button(),
me->buttons(),
me->modifiers());
mouseReleaseEvent(&newEvent);
return true;
}
default:
break;
}
}
}
return QToolBar::eventFilter(watched, event);
}
void KToolBar::actionEvent(QActionEvent *event)
{
if (event->type() == QEvent::ActionRemoved) {
QWidget *widget = widgetForAction(event->action());
if (widget) {
widget->removeEventFilter(this);
const auto children = widget->findChildren<QWidget *>();
for (QWidget *child : children) {
child->removeEventFilter(this);
}
}
}
QToolBar::actionEvent(event);
if (event->type() == QEvent::ActionAdded) {
QWidget *widget = widgetForAction(event->action());
if (widget) {
widget->installEventFilter(this);
const auto children = widget->findChildren<QWidget *>();
for (QWidget *child : children) {
child->installEventFilter(this);
}
// Center widgets that do not have any use for more space. See bug 165274
if (!(widget->sizePolicy().horizontalPolicy() & QSizePolicy::GrowFlag)
// ... but do not center when using text besides icon in vertical toolbar. See bug 243196
&& !(orientation() == Qt::Vertical && toolButtonStyle() == Qt::ToolButtonTextBesideIcon)) {
const int index = layout()->indexOf(widget);
if (index != -1) {
layout()->itemAt(index)->setAlignment(Qt::AlignJustify);
}
}
}
}
d->adjustSeparatorVisibility();
}
bool KToolBar::toolBarsEditable()
{
return KToolBarPrivate::s_editable;
}
void KToolBar::setToolBarsEditable(bool editable)
{
if (KToolBarPrivate::s_editable != editable) {
KToolBarPrivate::s_editable = editable;
}
}
void KToolBar::setToolBarsLocked(bool locked)
{
if (KToolBarPrivate::s_locked != locked) {
KToolBarPrivate::s_locked = locked;
const auto windows = KMainWindow::memberList();
for (KMainWindow *mw : windows) {
const auto toolbars = mw->findChildren<KToolBar *>();
for (KToolBar *toolbar : toolbars) {
toolbar->d->setLocked(locked);
}
}
}
}
bool KToolBar::toolBarsLocked()
{
return KToolBarPrivate::s_locked;
}
void KToolBar::emitToolbarStyleChanged()
{
#ifdef WITH_QTDBUS
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KToolBar"), QStringLiteral("org.kde.KToolBar"), QStringLiteral("styleChanged"));
QDBusConnection::sessionBus().send(message);
#endif
}
#include "moc_ktoolbar.cpp"
|