10#include "kedittoolbar.h"
12#include "kedittoolbar_p.h"
15#include <QApplication>
18#include <QDialogButtonBox>
20#include <QDomDocument>
28#include <QStandardPaths>
32#include <KListWidgetSearchLine>
33#include <KLocalizedString>
37#include "kactioncollection.h"
39#include "kxmlguifactory.h"
41#include "ktoolbarhelper_p.h"
42#include <kxmlgui_version.h>
53static ToolBarList findToolBars(
const QDomElement &
start)
57 for (QDomElement elem =
start; !elem.isNull(); elem = elem.nextSiblingElement()) {
58 if (elem.tagName() == QLatin1String(
"ToolBar")) {
59 if (elem.attribute(QStringLiteral(
"noEdit")) != QLatin1String(
"true")) {
63 if (elem.tagName() != QLatin1String(
"MenuBar")) {
64 list += findToolBars(elem.firstChildElement());
75 enum XmlType { Shell = 0, Part, Local, Merged };
77 explicit XmlData(XmlType xmlType,
const QString &xmlFile, KActionCollection *collection)
81 , m_actionCollection(collection)
87 qDebug(240) <<
"XmlData" <<
this <<
"type" << s_XmlTypeToString[m_type] <<
"xmlFile:" << m_xmlFile;
88 foreach (
const QDomElement &element, m_barList) {
89 qDebug(240) <<
" ToolBar:" << toolBarText(element);
91 if (m_actionCollection) {
92 qDebug(240) <<
" " << m_actionCollection->actions().count() <<
"actions in the collection.";
94 qDebug(240) <<
" no action collection.";
98 QString xmlFile()
const
106 KActionCollection *actionCollection()
const
108 return m_actionCollection;
110 void setDomDocument(
const QDomDocument &domDoc)
113 m_barList = findToolBars(m_document.documentElement());
116 QDomDocument &domDocument()
120 const QDomDocument &domDocument()
const
128 QString toolBarText(
const QDomElement &it)
const;
130 bool m_isModified =
false;
131 ToolBarList &barList()
135 const ToolBarList &barList()
const
141 ToolBarList m_barList;
143 QDomDocument m_document;
145 KActionCollection *m_actionCollection;
148QString XmlData::toolBarText(
const QDomElement &it)
const
150 QString
name = KToolbarHelper::i18nToolBarName(it);
154 if ((m_type == XmlData::Shell) || (m_type == XmlData::Part)) {
155 QString doc_name(m_document.documentElement().attribute(QStringLiteral(
"name")));
156 name += QLatin1String(
" <") + doc_name + QLatin1Char(
'>');
161typedef QList<XmlData> XmlDataList;
163class ToolBarItem :
public QListWidgetItem
166 ToolBarItem(QListWidget *parent,
const QString &tag = QString(),
const QString &name = QString(),
const QString &statusText = QString())
169 , m_internalName(
name)
170 , m_statusText(statusText)
171 , m_isSeparator(false)
173 , m_isTextAlongsideIconHidden(false)
179 void setInternalTag(
const QString &tag)
183 void setInternalName(
const QString &name)
185 m_internalName =
name;
187 void setStatusText(
const QString &
text)
191 void setSeparator(
bool sep)
195 void setSpacer(
bool spacer)
199 void setTextAlongsideIconHidden(
bool hidden)
201 m_isTextAlongsideIconHidden = hidden;
203 QString internalTag()
const
205 return m_internalTag;
207 QString internalName()
const
209 return m_internalName;
211 QString statusText()
const
215 bool isSeparator()
const
217 return m_isSeparator;
219 bool isSpacer()
const
223 bool isTextAlongsideIconHidden()
const
225 return m_isTextAlongsideIconHidden;
234 QString m_internalTag;
235 QString m_internalName;
236 QString m_statusText;
239 bool m_isTextAlongsideIconHidden;
242static QDataStream &operator<<(QDataStream &s,
const ToolBarItem &item)
244 s << item.internalTag();
245 s << item.internalName();
246 s << item.statusText();
247 s << item.isSeparator();
248 s << item.isSpacer();
249 s << item.isTextAlongsideIconHidden();
252static QDataStream &operator>>(QDataStream &s, ToolBarItem &item)
256 item.setInternalTag(internalTag);
257 QString internalName;
259 item.setInternalName(internalName);
262 item.setStatusText(statusText);
265 item.setSeparator(sep);
268 item.setSpacer(spacer);
271 item.setTextAlongsideIconHidden(hidden);
277ToolBarListWidget::ToolBarListWidget(QWidget *parent)
278 : QListWidget(parent)
294 ToolBarItem *item =
static_cast<ToolBarItem *
>(items.
first());
298 mimedata->
setData(QStringLiteral(
"application/x-kde-action-list"), data);
299 mimedata->
setData(QStringLiteral(
"application/x-kde-source-treewidget"), m_activeList ?
"active" :
"inactive");
307 const QByteArray data = mimeData->
data(QStringLiteral(
"application/x-kde-action-list"));
312 const bool sourceIsActiveList = mimeData->
data(QStringLiteral(
"application/x-kde-source-treewidget")) ==
"active";
313 ToolBarItem *item =
new ToolBarItem(
this);
315 Q_EMIT dropped(
this, index, item, sourceIsActiveList);
319ToolBarItem *ToolBarListWidget::currentItem()
const
324IconTextEditDialog::IconTextEditDialog(
QWidget *parent)
327 setWindowTitle(
i18nc(
"@title:window",
"Change Text"));
336 m_lineEdit->setClearButtonEnabled(
true);
338 label->setBuddy(m_lineEdit);
342 m_cbHidden =
new QCheckBox(
i18nc(
"@option:check",
"&Hide text when toolbar shows text alongside icons"),
this);
355 m_lineEdit->setFocus();
356 setFixedHeight(sizeHint().height());
359void IconTextEditDialog::setIconText(
const QString &text)
361 m_lineEdit->setText(text);
364QString IconTextEditDialog::iconText()
const
366 return m_lineEdit->text().
trimmed();
369void IconTextEditDialog::setTextAlongsideIconHidden(
bool hidden)
371 m_cbHidden->setChecked(hidden);
374bool IconTextEditDialog::textAlongsideIconHidden()
const
376 return m_cbHidden->isChecked();
379void IconTextEditDialog::slotTextChanged(
const QString &text)
385class KEditToolBarWidgetPrivate
395 KEditToolBarWidgetPrivate(KEditToolBarWidget *widget,
const QString &cName, KActionCollection *collection)
396 : m_collection(collection)
399 , m_componentName(cName)
400 , m_helpArea(nullptr)
402 , m_loadedOnce(false)
407 m_emptyIcon = QPixmap(iconSize, iconSize);
410 ~KEditToolBarWidgetPrivate()
415 void slotToolBarSelected(
int index);
417 void slotInactiveSelectionChanged();
418 void slotActiveSelectionChanged();
420 void slotInsertButton();
421 void slotRemoveButton();
423 void slotDownButton();
425 void selectActiveItem(
const QString &);
427 void slotChangeIcon();
428 void slotChangeIconText();
430 void slotDropped(ToolBarListWidget *list,
int index, ToolBarItem *item,
bool sourceIsActiveList);
434 void initOldStyle(
const QString &file,
bool global,
const QString &defaultToolbar);
435 void initFromFactory(KXMLGUIFactory *factory,
const QString &defaultToolbar);
436 void loadToolBarCombo(
const QString &defaultToolbar);
437 void loadActions(
const QDomElement &elem);
439 QString xmlFile(
const QString &xml_file)
const
441 return xml_file.
isEmpty() ? m_componentName + QLatin1String(
"ui.rc") : xml_file;
447 QString loadXMLFile(
const QString &_xml_file)
450 QString xml_file = xmlFile(_xml_file);
465 QDomElement findElementForToolBarItem(
const ToolBarItem *item)
const
468 for (QDomNode n = m_currentToolBarElem.firstChild(); !n.isNull(); n = n.nextSibling()) {
469 QDomElement elem = n.toElement();
470 if ((elem.
attribute(QStringLiteral(
"name")) == item->internalName()) && (elem.
tagName() == item->internalTag())) {
475 return QDomElement();
478 void insertActive(ToolBarItem *item, ToolBarItem *before,
bool prepend =
false);
479 void removeActive(ToolBarItem *item);
480 void moveActive(ToolBarItem *item, ToolBarItem *before);
481 void updateLocal(QDomElement &elem);
486 for (
const auto &xmlFile : m_xmlFiles) {
492 QComboBox *m_toolbarCombo;
494 QToolButton *m_upAction;
495 QToolButton *m_removeAction;
496 QToolButton *m_insertAction;
497 QToolButton *m_downAction;
500 KActionCollection *m_collection;
501 KEditToolBarWidget *
const m_widget;
502 KXMLGUIFactory *m_factory;
503 QString m_componentName;
507 XmlData *m_currentXmlData;
508 QDomElement m_currentToolBarElem;
511 QString m_globalFile;
513 QDomDocument m_localDoc;
515 ToolBarList m_barList;
516 ToolBarListWidget *m_inactiveList;
517 ToolBarListWidget *m_activeList;
519 XmlDataList m_xmlFiles;
521 QLabel *m_comboLabel;
522 KSeparator *m_comboSeparator;
524 QPushButton *m_changeIcon;
525 QPushButton *m_changeIconText;
527 bool m_loadedOnce : 1;
532using namespace KDEPrivate;
534class KEditToolBarPrivate
537 KEditToolBarPrivate(KEditToolBar *qq)
544 void slotButtonClicked(QAbstractButton *button);
546 void enableApply(
bool);
549 void defaultClicked();
551 KEditToolBar *
const q;
552 bool m_accept =
false;
554 bool m_global =
false;
555 KActionCollection *m_collection =
nullptr;
557 QString m_defaultToolBar;
558 KXMLGUIFactory *m_factory =
nullptr;
559 KEditToolBarWidget *m_widget =
nullptr;
560 QVBoxLayout *m_layout =
nullptr;
561 QDialogButtonBox *m_buttonBox =
nullptr;
564Q_GLOBAL_STATIC(
QString, s_defaultToolBarName)
568 , d(new KEditToolBarPrivate(this))
570 d->m_widget =
new KEditToolBarWidget(collection,
this);
572 d->m_collection = collection;
577 , d(new KEditToolBarPrivate(this))
579 d->m_widget =
new KEditToolBarWidget(
this);
581 d->m_factory = factory;
584void KEditToolBarPrivate::init()
604 slotButtonClicked(button);
607 m_layout->addWidget(m_buttonBox);
609 q->connect(m_widget, &KEditToolBarWidget::enableOk, q, [
this](
bool state) {
615 q->setMinimumSize(q->sizeHint());
621 d->m_global = global;
622 d->m_widget->load(d->m_file, d->m_global, d->m_defaultToolBar);
627 s_defaultToolBarName()->clear();
633 d->m_defaultToolBar = *s_defaultToolBarName();
635 d->m_defaultToolBar = toolBarName;
639void KEditToolBarPrivate::acceptOK(
bool b)
645void KEditToolBarPrivate::enableApply(
bool b)
650void KEditToolBarPrivate::defaultClicked()
654 i18n(
"Do you really want to reset all toolbars of this application to their default? The changes will be applied immediately."),
655 i18n(
"Reset Toolbars"),
656 KGuiItem(
i18n(
"Reset")))
661 KEditToolBarWidget *oldWidget = m_widget;
666 const auto clients = m_factory->clients();
667 for (KXMLGUIClient *client : clients) {
668 const QString file = client->localXMLFile();
676 qCWarning(DEBUG_KXMLGUI) <<
"Could not delete" << file;
682 oldWidget->rebuildKXMLGUIClients();
684 m_widget =
new KEditToolBarWidget(q);
685 m_widget->load(m_factory, m_defaultToolBar);
687 int slash = m_file.lastIndexOf(QLatin1Char(
'/')) + 1;
689 m_file.remove(0, slash);
696 qCWarning(DEBUG_KXMLGUI) <<
"Could not delete " << xml_file;
700 m_widget =
new KEditToolBarWidget(m_collection, q);
701 q->setResourceFile(m_file, m_global);
705 m_widget->setGeometry(oldWidget->geometry());
707 m_layout->insertWidget(0, m_widget);
709 q->connect(m_widget, &KEditToolBarWidget::enableOk, q, [
this](
bool state) {
715 Q_EMIT q->newToolBarConfig();
737void KEditToolBarPrivate::okClicked()
748 Q_EMIT q->newToolBarConfig();
753void KEditToolBarPrivate::applyClicked()
755 (void)m_widget->save();
757 Q_EMIT q->newToolBarConfig();
762 *s_defaultToolBarName() = toolBarName;
767 , d(new KEditToolBarWidgetPrivate(this, componentName(), collection))
772KEditToolBarWidget::KEditToolBarWidget(
QWidget *parent)
774 , d(new KEditToolBarWidgetPrivate(this, componentName(),
KXMLGUIClient::actionCollection() ))
779KEditToolBarWidget::~KEditToolBarWidget()
784void KEditToolBarWidget::load(
const QString &file,
bool global,
const QString &defaultToolBar)
786 d->initOldStyle(file, global, defaultToolBar);
791 d->initFromFactory(factory, defaultToolBar);
794void KEditToolBarWidgetPrivate::initOldStyle(
const QString &resourceFile,
bool global,
const QString &defaultToolBar)
806 m_widget->loadStandardsXmlFile();
808 const QString localXML = loadXMLFile(resourceFile);
809 m_widget->setXML(localXML, global ?
true :
false);
812 XmlData local(XmlData::Local, xmlFile(resourceFile), m_collection);
815 local.setDomDocument(domDoc);
816 m_xmlFiles.append(local);
819 XmlData
merge(XmlData::Merged, QString(), m_collection);
820 merge.setDomDocument(m_widget->domDocument());
821 m_xmlFiles.append(merge);
828 loadToolBarCombo(defaultToolBar);
829 m_widget->adjustSize();
830 m_widget->setMinimumSize(m_widget->sizeHint());
833void KEditToolBarWidgetPrivate::initFromFactory(KXMLGUIFactory *factory,
const QString &defaultToolBar)
846 const auto clients = factory->
clients();
847 for (KXMLGUIClient *client : clients) {
848 if (client->xmlFile().isEmpty()) {
852 XmlData::XmlType
type = XmlData::Part;
854 type = XmlData::Shell;
856 Q_ASSERT(!client->localXMLFile().isEmpty());
859 XmlData data(type, client->localXMLFile(), client->actionCollection());
860 QDomDocument domDoc = client->domDocument();
861 data.setDomDocument(domDoc);
872 loadToolBarCombo(defaultToolBar);
873 m_widget->adjustSize();
874 m_widget->setMinimumSize(m_widget->sizeHint());
876 m_widget->actionCollection()->addAssociatedWidget(m_widget);
877 const auto widgetActions = m_widget->actionCollection()->actions();
878 for (QAction *action : widgetActions) {
883void KEditToolBarWidget::save()
886 for (
const auto &xmlFile : std::as_const(d->m_xmlFiles)) {
888 if (!xmlFile.m_isModified) {
893 if (xmlFile.type() == XmlData::Merged) {
898 QDomNodeList menuNodes = xmlFile.domDocument().elementsByTagName(QStringLiteral(
"Menu"));
899 for (
int i = 0; i < menuNodes.
length(); ++i) {
900 QDomNode menuNode = menuNodes.
item(i);
901 QDomElement menuElement = menuNode.
toElement();
902 if (menuElement.
isNull()) {
905 menuElement.
setAttribute(QStringLiteral(
"noMerge"), QStringLiteral(
"1"));
919 rebuildKXMLGUIClients();
922void KEditToolBarWidget::rebuildKXMLGUIClients()
928 const QList<KXMLGUIClient *> clients = d->m_factory->clients();
936 for (
auto it = clients.
crbegin(); it != clients.
crend(); ++it) {
938 d->m_factory->removeClient(*it);
941 KXMLGUIClient *firstClient = clients.
first();
945 for (KXMLGUIClient *client : clients) {
947 QString file(client->xmlFile());
950 client->setXMLGUIBuildDocument(QDomDocument());
953 if (client == firstClient) {
954 client->loadStandardsXmlFile();
958 client->setXMLFile(file, client == firstClient );
967 for (KXMLGUIClient *client : clients) {
968 d->m_factory->addClient(client);
972void KEditToolBarWidgetPrivate::setupLayout()
975 m_comboLabel =
new QLabel(
i18n(
"&Toolbar:"), m_widget);
976 m_toolbarCombo =
new QComboBox(m_widget);
977 m_comboLabel->setBuddy(m_toolbarCombo);
978 m_comboSeparator =
new KSeparator(m_widget);
980 slotToolBarSelected(index);
991 QLabel *inactive_label =
new QLabel(
i18n(
"A&vailable actions:"), m_widget);
992 m_inactiveList =
new ToolBarListWidget(m_widget);
993 m_inactiveList->setDragEnabled(
true);
994 m_inactiveList->setActiveList(
false);
995 m_inactiveList->setMinimumSize(180, 200);
996 m_inactiveList->setDropIndicatorShown(
false);
997 inactive_label->setBuddy(m_inactiveList);
999 slotInactiveSelectionChanged();
1005 &ToolBarListWidget::dropped,
1007 [
this](ToolBarListWidget *list,
int index, ToolBarItem *item,
bool sourceIsActiveList) {
1008 slotDropped(list, index, item, sourceIsActiveList);
1011 KListWidgetSearchLine *inactiveListSearchLine =
new KListWidgetSearchLine(m_widget, m_inactiveList);
1012 inactiveListSearchLine->setPlaceholderText(
i18n(
"Filter"));
1015 QLabel *active_label =
new QLabel(
i18n(
"Curr&ent actions:"), m_widget);
1016 m_activeList =
new ToolBarListWidget(m_widget);
1017 m_activeList->setDragEnabled(
true);
1018 m_activeList->setActiveList(
true);
1020 m_activeList->setMinimumSize(m_inactiveList->minimumWidth(), 100);
1021 active_label->
setBuddy(m_activeList);
1024 slotActiveSelectionChanged();
1030 &ToolBarListWidget::dropped,
1032 [
this](ToolBarListWidget *list,
int index, ToolBarItem *item,
bool sourceIsActiveList) {
1033 slotDropped(list, index, item, sourceIsActiveList);
1036 KListWidgetSearchLine *activeListSearchLine =
new KListWidgetSearchLine(m_widget, m_activeList);
1037 activeListSearchLine->setPlaceholderText(
i18n(
"Filter"));
1040 m_changeIcon =
new QPushButton(
i18nc(
"@action:button",
"Change &Icon…"), m_widget);
1041 m_changeIcon->setIcon(
QIcon::fromTheme(QStringLiteral(
"preferences-desktop-icons")));
1042 m_changeIcon->setEnabled(m_activeList->currentItem());
1049 m_changeIconText =
new QPushButton(
i18nc(
"@action:button",
"Change Te&xt…"), m_widget);
1050 m_changeIconText->setIcon(
QIcon::fromTheme(QStringLiteral(
"edit-rename")));
1051 m_changeIconText->setEnabled(m_activeList->currentItem() !=
nullptr);
1054 slotChangeIconText();
1059 m_upAction =
new QToolButton(m_widget);
1061 m_upAction->setEnabled(
false);
1062 m_upAction->setAutoRepeat(
true);
1067 m_insertAction =
new QToolButton(m_widget);
1069 m_insertAction->setEnabled(
false);
1074 m_removeAction =
new QToolButton(m_widget);
1076 m_removeAction->setEnabled(
false);
1081 m_downAction =
new QToolButton(m_widget);
1083 m_downAction->setEnabled(
false);
1084 m_downAction->setAutoRepeat(
true);
1089 m_helpArea =
new QLabel(m_widget);
1090 m_helpArea->setWordWrap(
true);
1093 QVBoxLayout *top_layout =
new QVBoxLayout(m_widget);
1096 QVBoxLayout *name_layout =
new QVBoxLayout();
1097 QHBoxLayout *list_layout =
new QHBoxLayout();
1099 QVBoxLayout *inactive_layout =
new QVBoxLayout();
1100 QVBoxLayout *active_layout =
new QVBoxLayout();
1101 QHBoxLayout *changeIcon_layout =
new QHBoxLayout();
1103 QGridLayout *button_layout =
new QGridLayout();
1112 button_layout->
addWidget(m_upAction, 1, 1);
1113 button_layout->
addWidget(m_removeAction, 2, 0);
1114 button_layout->
addWidget(m_insertAction, 2, 2);
1115 button_layout->
addWidget(m_downAction, 3, 1);
1118 inactive_layout->
addWidget(inactive_label);
1119 inactive_layout->
addWidget(inactiveListSearchLine);
1120 inactive_layout->
addWidget(m_inactiveList, 1);
1123 active_layout->
addWidget(activeListSearchLine);
1124 active_layout->
addWidget(m_activeList, 1);
1125 active_layout->
addLayout(changeIcon_layout);
1127 changeIcon_layout->
addWidget(m_changeIcon);
1129 changeIcon_layout->
addWidget(m_changeIconText);
1131 list_layout->
addLayout(inactive_layout);
1136 top_layout->
addWidget(m_comboSeparator);
1139 top_layout->
addWidget(
new KSeparator(m_widget));
1142void KEditToolBarWidgetPrivate::loadToolBarCombo(
const QString &defaultToolBar)
1144 const QLatin1String attrName(
"name");
1146 m_toolbarCombo->clear();
1148 int defaultToolBarId = -1;
1151 for (
const auto &xmlFile : std::as_const(m_xmlFiles)) {
1155 if (xmlFile.type() == XmlData::Merged) {
1160 for (
const auto &bar : std::as_const(xmlFile.barList())) {
1161 const QString text = xmlFile.toolBarText(bar);
1162 m_toolbarCombo->addItem(text);
1163 const QString
name = bar.attribute(attrName);
1164 if (defaultToolBarId == -1 && name == defaultToolBar) {
1165 defaultToolBarId = count;
1170 const bool showCombo = (count > 1);
1171 m_comboLabel->setVisible(showCombo);
1172 m_comboSeparator->setVisible(showCombo);
1173 m_toolbarCombo->setVisible(showCombo);
1174 if (defaultToolBarId == -1) {
1175 defaultToolBarId = 0;
1178 m_toolbarCombo->setCurrentIndex(defaultToolBarId);
1179 slotToolBarSelected(m_toolbarCombo->currentIndex());
1182void KEditToolBarWidgetPrivate::loadActions(
const QDomElement &elem)
1184 const QLatin1String tagSeparator(
"Separator");
1185 const QLatin1String tagSpacer(
"Spacer");
1186 const QLatin1String tagMerge(
"Merge");
1187 const QLatin1String tagActionList(
"ActionList");
1188 const QLatin1String tagAction(
"Action");
1189 const QLatin1String attrName(
"name");
1191 const QString separatorstring =
i18n(
"--- separator ---");
1192 const QString spacerstring =
i18n(
"--- expanding spacer ---");
1195 QString sep_name(QStringLiteral(
"separator_%1"));
1197 QString spacer_name(QStringLiteral(
"spacer_%1"));
1200 m_inactiveList->clear();
1201 m_activeList->clear();
1202 m_insertAction->setEnabled(
false);
1203 m_removeAction->setEnabled(
false);
1204 m_upAction->setEnabled(
false);
1205 m_downAction->setEnabled(
false);
1208 KActionCollection *actionCollection = m_currentXmlData->actionCollection();
1211 QSet<QString> active_list;
1214 KLocalizedString nameFilter =
ki18nc(
"@item:intable Action name in toolbar editor",
"%1");
1223 if (it.
tagName() == tagSeparator) {
1224 ToolBarItem *act =
new ToolBarItem(m_activeList, tagSeparator, sep_name.arg(sep_num++), QString());
1225 act->setSeparator(
true);
1226 act->
setText(separatorstring);
1230 if (it.
tagName() == tagSpacer) {
1231 ToolBarItem *act =
new ToolBarItem(m_activeList, tagSpacer, spacer_name.arg(spacer_num++), QString());
1232 act->setSpacer(
true);
1238 if (it.
tagName() == tagMerge) {
1242 new ToolBarItem(m_activeList, tagMerge, name,
i18n(
"This element will be replaced with all the elements of an embedded component."));
1251 if (it.
tagName() == tagActionList) {
1253 new ToolBarItem(m_activeList,
1256 i18n(
"This is a dynamic list of actions. You can move it, but if you remove it you will not be able to re-add it."));
1264 const auto actions = actionCollection->
actions();
1265 for (QAction *action : actions) {
1267 if (it.
attribute(attrName) == action->objectName()) {
1269 ToolBarItem *act =
new ToolBarItem(m_activeList, it.
tagName(), action->objectName(), action->toolTip());
1271 act->
setIcon(!action->icon().isNull() ? action->icon() : m_emptyIcon);
1274 active_list.
insert(action->objectName());
1281 const auto actions = actionCollection->
actions();
1282 for (QAction *action : actions) {
1284 if (active_list.
contains(action->objectName())) {
1288 ToolBarItem *act =
new ToolBarItem(m_inactiveList, tagAction, action->objectName(), action->toolTip());
1290 act->
setIcon(!action->icon().isNull() ? action->icon() : m_emptyIcon);
1296 ToolBarItem *sep =
new ToolBarItem(
nullptr, tagSeparator, sep_name.arg(sep_num++), QString());
1297 sep->setSeparator(
true);
1298 sep->
setText(separatorstring);
1299 m_inactiveList->insertItem(0, sep);
1301 ToolBarItem *spacer =
new ToolBarItem(
nullptr, tagSpacer, spacer_name.arg(spacer_num++), QString());
1302 spacer->setSpacer(
true);
1303 spacer->
setText(spacerstring);
1304 m_inactiveList->insertItem(1, spacer);
1307KActionCollection *KEditToolBarWidget::actionCollection()
const
1309 return d->m_collection;
1312void KEditToolBarWidgetPrivate::slotToolBarSelected(
int index)
1317 int toolbarNumber = 0;
1318 for (
auto &xmlFile : m_xmlFiles) {
1321 if (xmlFile.type() == XmlData::Merged) {
1326 const auto &barList = xmlFile.barList();
1327 for (
const auto &bar : barList) {
1329 if (toolbarNumber == index) {
1331 m_currentXmlData = &xmlFile;
1332 m_currentToolBarElem = bar;
1335 m_currentXmlData->dump();
1338 Q_ASSERT(m_currentXmlData->type() != XmlData::Merged);
1341 loadActions(m_currentToolBarElem);
1343 if (xmlFile.type() == XmlData::Part || xmlFile.type() == XmlData::Shell) {
1344 m_widget->setDOMDocument(xmlFile.domDocument());
1353void KEditToolBarWidgetPrivate::slotInactiveSelectionChanged()
1355 if (!m_inactiveList->selectedItems().isEmpty()) {
1356 m_insertAction->setEnabled(
true);
1357 QString statusText =
static_cast<ToolBarItem *
>(m_inactiveList->selectedItems().first())->statusText();
1358 m_helpArea->setText(
i18nc(
"@label Action tooltip in toolbar editor, below the action list",
"%1", statusText));
1360 m_insertAction->setEnabled(
false);
1361 m_helpArea->setText(QString());
1365void KEditToolBarWidgetPrivate::slotActiveSelectionChanged()
1367 ToolBarItem *toolitem =
nullptr;
1368 if (!m_activeList->selectedItems().isEmpty()) {
1369 toolitem =
static_cast<ToolBarItem *
>(m_activeList->selectedItems().first());
1372 m_removeAction->setEnabled(toolitem);
1374 m_changeIcon->setEnabled(toolitem && toolitem->internalTag() == QLatin1String(
"Action"));
1376 m_changeIconText->setEnabled(toolitem && toolitem->internalTag() == QLatin1String(
"Action"));
1379 m_upAction->setEnabled(toolitem->index() != 0);
1380 m_downAction->setEnabled(toolitem->index() != toolitem->
listWidget()->
count() - 1);
1382 QString statusText = toolitem->statusText();
1383 m_helpArea->setText(
i18nc(
"@label Action tooltip in toolbar editor, below the action list",
"%1", statusText));
1385 m_upAction->setEnabled(
false);
1386 m_downAction->setEnabled(
false);
1387 m_helpArea->setText(QString());
1391void KEditToolBarWidgetPrivate::slotInsertButton()
1393 QString internalName =
static_cast<ToolBarItem *
>(m_inactiveList->currentItem())->internalName();
1395 insertActive(m_inactiveList->currentItem(), m_activeList->currentItem(),
false);
1397 Q_EMIT m_widget->enableOk(
true);
1399 slotToolBarSelected(m_toolbarCombo->currentIndex());
1401 selectActiveItem(internalName);
1404void KEditToolBarWidgetPrivate::selectActiveItem(
const QString &internalName)
1406 int activeItemCount = m_activeList->count();
1407 for (
int i = 0; i < activeItemCount; i++) {
1408 ToolBarItem *item =
static_cast<ToolBarItem *
>(m_activeList->item(i));
1409 if (item->internalName() == internalName) {
1410 m_activeList->setCurrentItem(item);
1416void KEditToolBarWidgetPrivate::slotRemoveButton()
1418 removeActive(m_activeList->currentItem());
1420 slotToolBarSelected(m_toolbarCombo->currentIndex());
1423void KEditToolBarWidgetPrivate::insertActive(ToolBarItem *item, ToolBarItem *before,
bool prepend)
1429 QDomElement new_item;
1431 if (item->isSeparator()) {
1432 new_item = m_widget->domDocument().createElement(QStringLiteral(
"Separator"));
1433 }
else if (item->isSpacer()) {
1434 new_item = m_widget->domDocument().createElement(QStringLiteral(
"Spacer"));
1436 new_item = m_widget->domDocument().createElement(QStringLiteral(
"Action"));
1439 new_item.
setAttribute(QStringLiteral(
"name"), item->internalName());
1441 Q_ASSERT(!m_currentToolBarElem.isNull());
1446 QDomElement elem = findElementForToolBarItem(before);
1447 Q_ASSERT(!elem.
isNull());
1448 m_currentToolBarElem.insertAfter(new_item, elem);
1452 m_currentToolBarElem.insertBefore(new_item, m_currentToolBarElem.firstChild());
1454 m_currentToolBarElem.appendChild(new_item);
1459 m_currentToolBarElem.setAttribute(QStringLiteral(
"noMerge"), QStringLiteral(
"1"));
1462 updateLocal(m_currentToolBarElem);
1465void KEditToolBarWidgetPrivate::removeActive(ToolBarItem *item)
1472 Q_EMIT m_widget->enableOk(
true);
1475 QDomElement elem = findElementForToolBarItem(item);
1478 m_currentToolBarElem.removeChild(elem);
1481 m_currentToolBarElem.setAttribute(QStringLiteral(
"noMerge"), QStringLiteral(
"1"));
1484 updateLocal(m_currentToolBarElem);
1488void KEditToolBarWidgetPrivate::slotUpButton()
1490 ToolBarItem *item = m_activeList->currentItem();
1505 Q_EMIT m_widget->enableOk(
true);
1507 moveActive(item,
static_cast<ToolBarItem *
>(item->
listWidget()->
item(row - 1)));
1510void KEditToolBarWidgetPrivate::moveActive(ToolBarItem *item, ToolBarItem *before)
1512 QDomElement e = findElementForToolBarItem(item);
1519 m_activeList->takeItem(m_activeList->row(item));
1522 m_activeList->insertItem(m_activeList->row(before) + 1, item);
1525 m_activeList->setCurrentItem(item);
1529 m_currentToolBarElem.insertBefore(e, m_currentToolBarElem.firstChild());
1531 m_currentToolBarElem.insertAfter(e, findElementForToolBarItem(before));
1535 m_currentToolBarElem.setAttribute(QStringLiteral(
"noMerge"), QStringLiteral(
"1"));
1538 updateLocal(m_currentToolBarElem);
1541void KEditToolBarWidgetPrivate::slotDownButton()
1543 ToolBarItem *item = m_activeList->currentItem();
1558 Q_EMIT m_widget->enableOk(
true);
1560 moveActive(item,
static_cast<ToolBarItem *
>(item->
listWidget()->
item(newRow)));
1563void KEditToolBarWidgetPrivate::updateLocal(QDomElement &elem)
1565 for (
auto &xmlFile : m_xmlFiles) {
1566 if (xmlFile.type() == XmlData::Merged) {
1570 if (xmlFile.type() == XmlData::Shell || xmlFile.type() == XmlData::Part) {
1571 if (m_currentXmlData->xmlFile() == xmlFile.xmlFile()) {
1572 xmlFile.m_isModified =
true;
1579 xmlFile.m_isModified =
true;
1580 const QLatin1String attrName(
"name");
1581 for (
const auto &bar : std::as_const(xmlFile.barList())) {
1582 const QString
name(bar.attribute(attrName));
1583 const QString tag(bar.tagName());
1588 QDomElement toolbar = xmlFile.domDocument().documentElement().toElement();
1594 QDomElement toolbar = xmlFile.domDocument().documentElement().
toElement();
1595 Q_ASSERT(!toolbar.
isNull());
1600void KEditToolBarWidgetPrivate::slotChangeIcon()
1602 m_currentXmlData->dump();
1603 Q_ASSERT(m_currentXmlData->type() != XmlData::Merged);
1611 i18n(
"Change Icon"));
1617 ToolBarItem *item = m_activeList->currentItem();
1622 m_currentXmlData->m_isModified =
true;
1628 Q_ASSERT(!act_elem.
isNull());
1632 Q_EMIT m_widget->enableOk(
true);
1636void KEditToolBarWidgetPrivate::slotChangeIconText()
1638 m_currentXmlData->dump();
1639 ToolBarItem *item = m_activeList->currentItem();
1642 QString iconText = item->
text();
1643 bool hidden = item->isTextAlongsideIconHidden();
1645 IconTextEditDialog dialog(m_widget);
1646 dialog.setIconText(iconText);
1647 dialog.setTextAlongsideIconHidden(hidden);
1650 iconText = dialog.iconText();
1651 hidden = dialog.textAlongsideIconHidden();
1653 bool hiddenChanged = hidden != item->isTextAlongsideIconHidden();
1654 bool iconTextChanged = iconText != item->
text();
1656 if (!ok || (!hiddenChanged && !iconTextChanged)) {
1661 item->setTextAlongsideIconHidden(hidden);
1663 Q_ASSERT(m_currentXmlData->type() != XmlData::Merged);
1665 m_currentXmlData->m_isModified =
true;
1671 Q_ASSERT(!act_elem.
isNull());
1672 if (iconTextChanged) {
1673 act_elem.
setAttribute(QStringLiteral(
"iconText"), iconText);
1675 if (hiddenChanged) {
1680 Q_EMIT m_widget->enableOk(
true);
1684void KEditToolBarWidgetPrivate::slotDropped(ToolBarListWidget *list,
int index, ToolBarItem *item,
bool sourceIsActiveList)
1688 if (list == m_activeList) {
1689 ToolBarItem *after = index > 0 ?
static_cast<ToolBarItem *
>(
list->item(index - 1)) : nullptr;
1691 if (sourceIsActiveList) {
1693 moveActive(item, after);
1696 insertActive(item, after,
true);
1698 }
else if (list == m_inactiveList) {
1706 Q_EMIT m_widget->enableOk(
true);
1708 slotToolBarSelected(m_toolbarCombo->currentIndex());
1711void KEditToolBar::showEvent(QShowEvent *event)
1713 if (!
event->spontaneous()) {
1717 d->m_widget->load(d->m_factory, d->m_defaultToolBar);
1720 d->m_widget->load(d->m_file, d->m_global, d->m_defaultToolBar);
1728void KEditToolBar::hideEvent(QHideEvent *event)
1736#include "moc_kedittoolbar.cpp"
1737#include "moc_kedittoolbar_p.cpp"
A container for a set of QAction objects.
QList< QAction * > actions() const
Returns the list of QActions which belong to this action collection.
static void assign(QPushButton *button, const KGuiItem &item)
static QString getIcon(KIconLoader::Group group=KIconLoader::Desktop, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, QWidget *parent=nullptr, const QString &title=QString())
static QString removeAcceleratorMarker(const QString &label)
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
KXMLGUIFactory, together with KXMLGUIClient objects, can be used to create a GUI of container widgets...
static QString readConfigFile(const QString &filename, const QString &componentName=QString())
QList< KXMLGUIClient * > clients() const
Returns a list of all clients currently added to this factory.
static bool saveConfigFile(const QDomDocument &doc, const QString &filename, const QString &componentName=QString())
static QDomElement actionPropertiesElement(QDomDocument &doc)
static QDomElement findActionByName(QDomElement &elem, const QString &sName, bool create)
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
QStringView merge(QStringView lhs, QStringView rhs)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
QString label(StandardShortcut id)
void addLayout(QLayout *layout, int stretch)
void addStretch(int stretch)
QByteArray & append(QByteArrayView data)
bool isEmpty() const const
void activated(int index)
QCoreApplication * instance()
QDialog(QWidget *parent, Qt::WindowFlags f)
void setModal(bool modal)
virtual void showEvent(QShowEvent *event) override
bool isRelativePath(const QString &path)
ParseResult setContent(QAnyStringView text, ParseOptions options)
QString attribute(const QString &name, const QString &defValue) const const
void setAttribute(const QString &name, const QString &value)
QString tagName() const const
QDomNode appendChild(const QDomNode &newChild)
QDomNode cloneNode(bool deep) const const
QDomNode firstChild() const const
bool isNull() const const
QDomNode nextSibling() const const
QDomNode replaceChild(const QDomNode &newChild, const QDomNode &oldChild)
QDomDocument toDocument() const const
QDomElement toElement() const const
QDomNode item(int index) const const
bool exists() const const
void setRowStretch(int row, int stretch)
virtual void setSpacing(int spacing) override
QIcon fromTheme(const QString &name)
void setBuddy(QWidget *buddy)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
void append(QList< T > &&value)
const_reverse_iterator crbegin() const const
const_reverse_iterator crend() const const
bool isEmpty() const const
void setText(const QString &text)
QString text() const const
QByteArray data(const QString &mimeType) const const
void setData(const QString &mimeType, const QByteArray &data)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
QString writableLocation(StandardLocation type)
bool isEmpty() const const
QString trimmed() const const
WidgetWithChildrenShortcut
QTestData & newRow(const char *dataTag)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)