[rkward/work/preview_with_menu] rkward: Move RKXMLGUIPreviewArea to a proper file.

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Wed Feb 3 19:38:35 UTC 2016


Git commit a969a2e39facaa3c735641fcd25764b08d43f78d by Thomas Friedrichsmeier.
Committed on 03/02/2016 at 19:23.
Pushed by tfry into branch 'work/preview_with_menu'.

Move RKXMLGUIPreviewArea to a proper file.
Do not show inactive actions in the preview area's menu.

M  +1    -0    rkward/misc/CMakeLists.txt
A  +116  -0    rkward/misc/rkxmlguipreviewarea.cpp     [License: GPL (v2+)]
A  +46   -0    rkward/misc/rkxmlguipreviewarea.h     [License: GPL (v2+)]
M  +1    -82   rkward/plugin/rkpreviewbox.cpp

http://commits.kde.org/rkward/a969a2e39facaa3c735641fcd25764b08d43f78d

diff --git a/rkward/misc/CMakeLists.txt b/rkward/misc/CMakeLists.txt
index 5417ff3..2e70517 100644
--- a/rkward/misc/CMakeLists.txt
+++ b/rkward/misc/CMakeLists.txt
@@ -28,6 +28,7 @@ SET(misc_STAT_SRCS
    rkfindbar.cpp
    rkdynamicsearchline.cpp
    rkaccordiontable.cpp
+   rkxmlguipreviewarea.cpp
    )
 
 QT4_AUTOMOC(${misc_STAT_SRCS})
diff --git a/rkward/misc/rkxmlguipreviewarea.cpp b/rkward/misc/rkxmlguipreviewarea.cpp
new file mode 100644
index 0000000..e4e5675
--- /dev/null
+++ b/rkward/misc/rkxmlguipreviewarea.cpp
@@ -0,0 +1,116 @@
+/***************************************************************************
+                          rkxmlguipreviewarea  -  description
+                             -------------------
+    begin                : Wed Feb 03 2016
+    copyright            : (C) 2016 by Thomas Friedrichsmeier
+    email                : thomas.friedrichsmeier at kdemail.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "rkxmlguipreviewarea.h"
+
+#include <QMenu>
+#include <QToolButton>
+#include <QEvent>
+#include <QMenuBar>
+#include <QWidgetAction>
+#include <QLabel>
+
+#include <kxmlguifactory.h>
+#include <ktoolbar.h>
+#include <kmenubar.h>
+#include <klocale.h>
+
+#include "../windows/rkmdiwindow.h"
+
+#include "../debug.h"
+
+RKXMLGUIPreviewArea::RKXMLGUIPreviewArea (QWidget* parent) : KXmlGuiWindow (parent) {
+	RK_TRACE (PLUGIN);
+
+	menu_button = new QToolButton (this);
+	menu_button->setPopupMode (QToolButton::InstantPopup);
+	menu_button->setIcon (QIcon::fromTheme ("menu_new"));
+	menu_button->setObjectName ("menubutton");
+	menu_button->setMenu (menu = new QMenu ());
+	// KF5 TODO:
+	connect (menu, SIGNAL (aboutToShow()), this, SLOT (prepareMenu()));
+	current = 0;
+	setWindowFlags (Qt::Widget);
+	setMenuBar (new QMenuBar (this));
+	setHelpMenuEnabled (false);
+}
+
+RKXMLGUIPreviewArea::~RKXMLGUIPreviewArea () {
+	RK_TRACE (PLUGIN);
+
+	if (current) {
+		removeChildClient (current);
+		current->setFactory (0);
+	}
+}
+
+void RKXMLGUIPreviewArea::childEvent (QChildEvent *event) {
+	RK_TRACE (PLUGIN);
+
+	if (event->type () == QEvent::ChildAdded) {
+		RKMDIWindow *child = qobject_cast<RKMDIWindow*> (event->child ());
+		if (child) {
+			if (current) {
+				removeChildClient (current);
+				factory ()->removeClient (current);  // _always_ remove before adding, or the previous child will be leaked in the factory
+			}
+			current = child->getPart ();
+			insertChildClient (current);
+			setCentralWidget (child);
+			createGUI ("rkdummypart.rc");
+			menuBar ()->hide ();
+			QList<KToolBar*> tbars = toolBars ();
+			for (int i = 0; i < tbars.size (); ++i) tbars[i]->hide ();
+		}
+	}
+	QObject::childEvent (event);
+}
+
+void RKXMLGUIPreviewArea::prepareMenu () {
+	RK_TRACE (PLUGIN);
+
+	// flatten menu, and try to purge irrelevant actions
+	menu->clear ();
+	QList<QAction*> entries = menuBar ()->actions ();
+	for (int i = 0; i < entries.size (); ++i) {
+		QMenu *smenu = entries[i]->menu ();
+		if (!smenu) continue;
+		QList<QAction*> subentries = smenu->actions ();
+		QList<QAction*> entries_to_add;
+		bool menu_empty = true;
+		for (int j = 0; j < subentries.size (); ++j) {
+			QAction *act = subentries[j];
+			if (act->isVisible () && act) {
+				entries_to_add.append (act);
+				if (!act->isSeparator ()) menu_empty = false;  // Copy separators, but purge menus with only separators in them.
+			}
+		}
+		if (menu_empty) continue;
+
+		QWidgetAction *act = new QWidgetAction (this);
+		QLabel *lab = new QLabel ("<b>" + entries[i]->text ().replace ('&', "") + "</b>");
+		lab->setAlignment (Qt::AlignCenter);
+		act->setDefaultWidget (lab);
+		menu->addAction (act);
+
+		for (int j = 0; j < entries_to_add.size (); ++j) {
+			menu->addAction (entries_to_add[j]);
+		}
+	}
+}
+
+#include "rkxmlguipreviewarea.moc"
diff --git a/rkward/misc/rkxmlguipreviewarea.h b/rkward/misc/rkxmlguipreviewarea.h
new file mode 100644
index 0000000..af3f4f5
--- /dev/null
+++ b/rkward/misc/rkxmlguipreviewarea.h
@@ -0,0 +1,46 @@
+/***************************************************************************
+                          rkxmlguipreviewarea  -  description
+                             -------------------
+    begin                : Wed Feb 03 2016
+    copyright            : (C) 2016 by Thomas Friedrichsmeier
+    email                : thomas.friedrichsmeier at kdemail.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef RKXMLGUIPREVIEWAREA_H
+#define RKXMLGUIPREVIEWAREA_H
+
+#include <kxmlguiwindow.h>
+#include <kparts/part.h>
+
+#include <QPointer>
+
+class QMenu;
+class QToolButton;
+
+class RKXMLGUIPreviewArea : public KXmlGuiWindow {
+	Q_OBJECT
+public:
+	explicit RKXMLGUIPreviewArea (QWidget* parent);
+	~RKXMLGUIPreviewArea ();
+protected:
+	/** build / destroy menu, when child is added removed. Note that we are in the fortunate situation that RKMDIWindow-children only ever get to the
+	 *  preview area via reparenting, i.e. contrary to usual QEvent::ChildAdded semnatics, they are always fully constructed, when added. */
+	void childEvent (QChildEvent *event);  // KF5 TODO: override keyword
+protected slots:
+	void prepareMenu ();
+private:
+	QToolButton *menu_button;
+	QMenu *menu;
+	QPointer<KParts::Part> current;
+};
+
+#endif
diff --git a/rkward/plugin/rkpreviewbox.cpp b/rkward/plugin/rkpreviewbox.cpp
index 691f144..b6c0c93 100644
--- a/rkward/plugin/rkpreviewbox.cpp
+++ b/rkward/plugin/rkpreviewbox.cpp
@@ -28,6 +28,7 @@
 #include "../rkglobals.h"
 #include "../rbackend/rinterface.h"
 #include "../misc/xmlhelper.h"
+#include "../misc/rkxmlguipreviewarea.h"
 #include "../windows/rkwindowcatcher.h"
 #include "../windows/rkworkplace.h"
 #include "rkstandardcomponent.h"
@@ -35,88 +36,6 @@
 
 #define DO_PREVIEW 102
 
-#include <kxmlguifactory.h>
-#include <QMenu>
-#include <QToolButton>
-#include <QEvent>
-#include <QMenuBar>
-#include <kxmlguiwindow.h>
-#include <ktoolbar.h>
-#include <kmenubar.h>
-#include <QPointer>
-#include <QWidgetAction>
-
-class RKXMLGUIPreviewArea : public KXmlGuiWindow {
-public:
-	explicit RKXMLGUIPreviewArea (QWidget* parent) : KXmlGuiWindow (parent) {
-		RK_TRACE (PLUGIN);
-		menu_button = new QToolButton (this);
-		menu_button->setPopupMode (QToolButton::InstantPopup);
-		menu_button->setIcon (QIcon::fromTheme ("menu_new"));
-		menu_button->setObjectName ("menubutton");
-		menu_button->setMenu (menu = new QMenu ());
-		current = 0;
-		setWindowFlags (Qt::Widget);
-		setMenuBar (new QMenuBar (this));
-		setHelpMenuEnabled (false);
-	}
-	~RKXMLGUIPreviewArea () {
-		RK_TRACE (PLUGIN);
-
-		if (current) {
-			removeChildClient (current);
-			current->setFactory (0);
-		}
-	}
-protected:
-	/** build / destroy menu, when child is added removed. Note that we are in the fortunate situation that RKMDIWindow-children only ever get to the
-	 *  preview area via reparenting, i.e. contrary to usual QEvent::ChildAdded semnatics, they are always fully constructed, when added. */
-	void childEvent (QChildEvent *event) {  // KF5 TODO: override keyword
-		RK_TRACE (PLUGIN);
-
-		if (event->type () == QEvent::ChildAdded) {
-			RKMDIWindow *child = qobject_cast<RKMDIWindow*> (event->child ());
-			if (child) {
-				if (current) {
-					removeChildClient (current);
-					factory ()->removeClient (current);  // _always_ remove before adding, or the previous child will be leaked in the factory
-				}
-				current = child->getPart ();
-				insertChildClient (current);
-				setCentralWidget (child);
-				createGUI ("rkdummypart.rc");
-				menuBar ()->hide ();
-				QList<KToolBar*> tbars = toolBars ();
-				for (int i = 0; i < tbars.size (); ++i) tbars[i]->hide ();
-				menu->clear ();
-				QList<QAction*> entries = menuBar ()->actions ();
-				for (int i = 0; i < entries.size (); ++i) {
-					QMenu *smenu = entries[i]->menu ();
-					if (!smenu) continue;
-					QList<QAction*> subentries = smenu->actions ();
-					if (subentries.isEmpty ()) continue;
-
-					QWidgetAction *act = new QWidgetAction (this);
-					QLabel *lab = new QLabel ("<b>" + entries[i]->text ().replace ('&', "") + "</b>");
-					lab->setAlignment (Qt::AlignCenter);
-					act->setDefaultWidget (lab);
-					menu->addAction (act);
-
-					for (int j = 0; j < subentries.size (); ++j) {
-						menu->addAction (subentries[j]);
-					}
-				}
-			}
-		}
-		QObject::childEvent (event);
-	}
-private:
-	QToolButton *menu_button;
-	QMenu *menu;
-	QPointer<KParts::Part> current;
-};
-
-
 RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_component, QWidget *parent_widget) : RKComponent (parent_component, parent_widget) {
 	RK_TRACE (PLUGIN);
 



More information about the rkward-tracker mailing list