[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kwrite-devel
Subject:    RGB color code plugin
From:       Jérôme_Lodewyck <lodewyck () free ! fr>
Date:       2009-01-10 11:02:04
Message-ID: 200901101202.04637.lodewyck () free ! fr
[Download RAW message or body]

Hi,

	I have written a small KTextEditor plugin that opens a color selection dialog 
and inserts the corresponding rgb color in HTML form (see attachment). If it 
is OK, I would like to commit it somewhere. Is there a place for such a 
plugin ?

Jérôme

["rgbcolor.diff" (text/x-diff)]

Index: ktexteditor_rgbcolorui.rc
===================================================================
--- ktexteditor_rgbcolorui.rc	(révision 0)
+++ ktexteditor_rgbcolorui.rc	(révision 0)
@@ -0,0 +1,9 @@
+<!DOCTYPE kpartgui>
+<gui name="ktexteditor_rgbcolor" library="ktexteditor_rgbcolor" version="1">
+<MenuBar>
+ <Menu name="tools"><text>&amp;Tools</text>
+    <Separator group="tools_operations" />
+    <Action name="tools_insert_rgbcolor" group="tools_operations"/>
+ </Menu>
+</MenuBar>
+</gui>
Index: ktexteditor_rgbcolor.desktop
===================================================================
--- ktexteditor_rgbcolor.desktop	(révision 0)
+++ ktexteditor_rgbcolor.desktop	(révision 0)
@@ -0,0 +1,20 @@
+[Desktop Entry]
+X-KDE-Library=ktexteditor_rgbcolor
+X-KDE-PluginInfo-Author=
+X-KDE-PluginInfo-Email=
+X-KDE-PluginInfo-Name=ktexteditorrgbcolor
+X-KDE-PluginInfo-Version=0.1
+X-KDE-PluginInfo-Website=http://kate.kde.org
+X-KDE-PluginInfo-Category=Editor
+X-KDE-PluginInfo-Depends=
+X-KDE-PluginInfo-License=GPL
+X-KDE-PluginInfo-EnabledByDefault=true
+X-KDE-ParentApp=kate
+X-KDE-Version=4.0
+X-KDE-ServiceTypes=KTextEditor/Plugin
+Type=Service
+Icon=fill-color
+Name=Insert RGB color code
+Name[fr]=Insérer un code couleur RGB
+Comment=Insert the selected RGB color code in HTML format
+Comment[fr]=Insérer le code couleur RGB sélectionné au format HTML
Index: rgbcolorplugin.cpp
===================================================================
--- rgbcolorplugin.cpp	(révision 0)
+++ rgbcolorplugin.cpp	(révision 0)
@@ -0,0 +1,115 @@
+/* This file is part of the KDE libraries
+   Copyright (C) 2008 Jérôme Lodewyck
+   Copyright (C) 2002 Anders Lund <anders@alweb.dk>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public
+   License version 2 as published by the Free Software Foundation.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#include "rgbcolorplugin.h"
+#include "rgbcolorplugin.moc"
+
+#include <ktexteditor/document.h>
+
+#include <assert.h>
+#include <kaction.h>
+#include <kactioncollection.h>
+#include <kcolordialog.h>
+#include <kpluginfactory.h>
+#include <kpluginloader.h>
+#include <klocale.h>
+#include <kmessagebox.h>
+#include <kpushbutton.h>
+#include <kaboutdata.h>
+
+#include <QtCore/QFile>
+#include <QtCore/QTextStream>
+
+K_PLUGIN_FACTORY(RgbColorPluginFactory, registerPlugin<RgbColorPlugin>();)
+K_EXPORT_PLUGIN(RgbColorPluginFactory(KAboutData( "ktexteditor_rgbcolor", \
"ktexteditor_plugins", ki18n("RGB Color"), "0.1", ki18n("Insert RGB color code"), \
KAboutData::License_LGPL_V2 ))) +
+//BEGIN RgbColorPlugin
+RgbColorPlugin::RgbColorPlugin(QObject* parent, const QVariantList&)
+	: KTextEditor::Plugin(parent)
+{
+}
+
+RgbColorPlugin::~RgbColorPlugin()
+{
+}
+
+void RgbColorPlugin::addView(KTextEditor::View *view)
+{
+	RgbColorPluginView *nview = new RgbColorPluginView(view, "RGB Color Plugin");
+	m_views.append(nview);
+}
+
+void RgbColorPlugin::removeView(KTextEditor::View *view)
+{
+	int z = 0;
+	// Loop written for the unlikely case of a view being added more than once
+	while (z < m_views.count())
+	{
+		RgbColorPluginView *nview = m_views.at(z);
+		if (nview->parentClient() == view)
+		{
+			m_views.removeAll(nview);
+			delete nview;
+		}
+		else
+			++z;
+	}
+}
+//END RgbColorPlugin
+
+//BEGIN RgbColorPluginView
+RgbColorPluginView::RgbColorPluginView(KTextEditor::View *view, const char *name)
+	: QObject(view)
+	, KXMLGUIClient(view)
+{
+	setObjectName(name);
+
+	setComponentData(RgbColorPluginFactory::componentData());
+
+	KAction *action = new KAction(KIcon("fill-color"), i18n("Insert RGB color \
code..."), this); +	actionCollection()->addAction("tools_insert_rgbcolor", action);
+	connect(action, SIGNAL(triggered(bool)), this, SLOT(slotInsertColor()));
+
+	setXMLFile("ktexteditor_rgbcolorui.rc");
+}
+
+void RgbColorPluginView::slotInsertColor()
+{
+	QColor color;
+
+	if (KColorDialog::getColor(color, (QWidget*)parent()) == QDialog::Accepted)
+		insertColor(color);
+}
+
+void RgbColorPluginView::insertColor(QColor& color)
+{
+	int r, g, b;
+	color.getRgb(&r, &g, &b);
+	QString colorCode;
+	colorCode.sprintf("#%02X%02X%02X", r, g, b);
+
+	KTextEditor::View* v = (KTextEditor::View*)parent();
+	if (v->selection())
+		v->document()->replaceText(v->selectionRange(), colorCode);
+	else
+		v->document()->insertText(v->cursorPosition(), colorCode);
+}
+
+//END RgbColorPluginView
+
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(révision 0)
+++ CMakeLists.txt	(révision 0)
@@ -0,0 +1,14 @@
+########### next target ###############
+
+set(ktexteditor_rgbcolor_PART_SRCS rgbcolorplugin.cpp )
+
+kde4_add_plugin(ktexteditor_rgbcolor ${ktexteditor_rgbcolor_PART_SRCS})
+
+target_link_libraries(ktexteditor_rgbcolor ${KDE4_KIO_LIBS} ktexteditor kdeui kfile)
+
+install(TARGETS  ktexteditor_rgbcolor  DESTINATION ${PLUGIN_INSTALL_DIR} )
+
+########### install files ###############
+
+install( FILES ktexteditor_rgbcolor.desktop  DESTINATION  ${SERVICES_INSTALL_DIR} )
+install( FILES ktexteditor_rgbcolorui.rc  DESTINATION  \
                ${DATA_INSTALL_DIR}/ktexteditor_rgbcolor )
Index: rgbcolorplugin.h
===================================================================
--- rgbcolorplugin.h	(révision 0)
+++ rgbcolorplugin.h	(révision 0)
@@ -0,0 +1,57 @@
+/* This file is part of the KDE libraries
+   Copyright (C) 2008 Jérôme Lodewyck
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public
+   License version 2 as published by the Free Software Foundation.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _RGBCOLOR_PLUGIN_H_
+#define _RGBCOLOR_PLUGIN_H_
+
+#include <ktexteditor/plugin.h>
+#include <ktexteditor/view.h>
+
+#include <kxmlguiclient.h>
+#include <QtCore/QObject>
+
+class RgbColorPlugin : public KTextEditor::Plugin
+{
+	Q_OBJECT
+
+	public:
+		explicit RgbColorPlugin(QObject *parent = 0,
+						const QVariantList &args = QVariantList());
+		virtual ~RgbColorPlugin();
+
+		void addView(KTextEditor::View *view);
+		void removeView(KTextEditor::View *view);
+
+	private:
+		QList<class RgbColorPluginView*> m_views;
+};
+
+class RgbColorPluginView : public QObject, public KXMLGUIClient
+{
+	Q_OBJECT
+	public:
+		explicit RgbColorPluginView(KTextEditor::View *view, const char *name = 0);
+		~RgbColorPluginView() {}
+	public Q_SLOTS:
+		// display a color dialog, and insert the chosen color
+		void slotInsertColor();
+	private:
+		void insertColor(QColor& color);
+};
+
+#endif // _RGBCOLOR_PLUGIN_H_



_______________________________________________
KWrite-Devel mailing list
KWrite-Devel@kde.org
https://mail.kde.org/mailman/listinfo/kwrite-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic