• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • interfaces
  • ktexteditor
editorchooser.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 * Copyright (C) 2005 Joseph Wenninger <jowenn@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19#include <editorchooser.h>
20#include <editorchooser.moc>
21
22#include <QtGui/QComboBox>
23#include <QtCore/QStringList>
24#include <QtGui/QLabel>
25#include <QtGui/QLayout>
26
27#include <kmimetypetrader.h>
28#include <kconfig.h>
29#include <klocale.h>
30#include <kglobal.h>
31
32#include "ui_editorchooser_ui.h"
33#include <kconfiggroup.h>
34
35using namespace KTextEditor;
36
37namespace KTextEditor
38{
39 class PrivateEditorChooser
40 {
41 public:
42 PrivateEditorChooser()
43 {
44 }
45 ~PrivateEditorChooser(){}
46 // Data Members
47 Ui::EditorChooser *chooser;
48 QStringList ElementNames;
49 QStringList elements;
50 };
51}
52
53EditorChooser::EditorChooser(QWidget *parent)
54 : QWidget(parent)
55{
56 d = new PrivateEditorChooser ();
57
58 d->chooser = new Ui::EditorChooser();
59 d->chooser->setupUi(this);
60
61 KService::List offers = KMimeTypeTrader::self()->query("text/plain", "KTextEditor/Document");
62 KConfigGroup config = KSharedConfig::openConfig("default_components")->group("KTextEditor");
63 QString editor = config.readPathEntry("embeddedEditor", QString());
64
65 if (editor.isEmpty()) editor = "katepart";
66
67 // search default component
68 for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it)
69 {
70 if ((*it)->desktopEntryName().contains(editor))
71 {
72 d->chooser->editorCombo->addItem(i18n("System Default (currently: %1)", (*it)->name()));
73 break;
74 }
75 }
76
77 // add list of all available components
78 for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it)
79 {
80 d->chooser->editorCombo->addItem((*it)->name());
81 d->elements.append((*it)->desktopEntryName());
82 }
83 d->chooser->editorCombo->setCurrentIndex(0);
84
85 connect(d->chooser->editorCombo,SIGNAL(activated(int)),this,SIGNAL(changed()));
86
87 setMinimumSize(sizeHint());
88}
89
90EditorChooser:: ~EditorChooser()
91{
92 delete d->chooser;
93 delete d;
94}
95
96void EditorChooser::readAppSetting(const QString& postfix)
97{
98 KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
99 QString editor = cg.readPathEntry("editor", QString());
100 if (editor.isEmpty())
101 d->chooser->editorCombo->setCurrentIndex(0);
102 else
103 {
104 // + 1, because item 0 is the default one.
105 int idx = d->elements.indexOf(editor) + 1;
106 d->chooser->editorCombo->setCurrentIndex(idx);
107 }
108}
109
110void EditorChooser::writeAppSetting(const QString& postfix)
111{
112 KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
113 cg.writeEntry("DEVELOPER_INFO","NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES");
114 cg.writePathEntry("editor", (d->chooser->editorCombo->currentIndex()<=0) ? //< for broken installations, where editor list is empty
115 QString() : QString(d->elements.at(d->chooser->editorCombo->currentIndex()-1)));
116}
117
118KTextEditor::Editor *EditorChooser::editor(const QString& postfix,
119 bool fallBackToKatePart)
120{
121 // try to read the used library from the application's config
122 KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
123 QString editor = cg.readPathEntry("editor", QString());
124 if (editor.isEmpty())
125 {
126 // there is no library set in the application's config,
127 // fall back to KDE's system default
128 KConfig config("default_components");
129 editor = config.group("KTextEditor").readPathEntry("embeddedEditor", "katepart");
130 }
131
132 KService::Ptr serv = KService::serviceByDesktopName(editor);
133 if (serv)
134 {
135 KTextEditor::Editor *tmpEd = KTextEditor::editor(serv->library().toLatin1());
136 if (tmpEd) return tmpEd;
137 }
138 if (fallBackToKatePart)
139 return KTextEditor::editor("katepart");
140
141 return 0;
142}
143
144// kate: space-indent on; indent-width 2; replace-tabs on;
KConfigGroup
KConfigGroup::writeEntry
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
KConfigGroup::readPathEntry
QString readPathEntry(const char *key, const QString &aDefault) const
KConfigGroup::writePathEntry
void writePathEntry(const char *pKey, const QString &path, WriteConfigFlags pFlags=Normal)
KConfig
KMimeTypeTrader::query
KService::List query(const QString &mimeType, const QString &genericServiceType=QString::fromLatin1("Application"), const QString &constraint=QString()) const
KMimeTypeTrader::self
static KMimeTypeTrader * self()
KService::serviceByDesktopName
static Ptr serviceByDesktopName(const QString &_name)
KSharedConfig::openConfig
static KSharedConfig::Ptr openConfig(const KComponentData &componentData, const QString &fileName=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
KSharedPtr< KService >
KTextEditor::EditorChooser::PrivateEditorChooser
friend class PrivateEditorChooser
Definition: editorchooser.h:90
KTextEditor::EditorChooser::editor
static KTextEditor::Editor * editor(const QString &postfix=QString(), bool fallBackToKatePart=true)
Static accessor to get the Editor instance of the currently used KTextEditor component.
Definition: editorchooser.cpp:118
KTextEditor::EditorChooser::EditorChooser
EditorChooser(QWidget *parent=0)
Constructor.
Definition: editorchooser.cpp:53
KTextEditor::EditorChooser::~EditorChooser
virtual ~EditorChooser()
Destructor.
Definition: editorchooser.cpp:90
KTextEditor::EditorChooser::writeAppSetting
void writeAppSetting(const QString &postfix=QString())
Write the configuration to the application's config file.
Definition: editorchooser.cpp:110
KTextEditor::EditorChooser::readAppSetting
void readAppSetting(const QString &postfix=QString())
Read the configuration from the application's config file.
Definition: editorchooser.cpp:96
KTextEditor::EditorChooser::changed
void changed()
This signal is emitted whenever the selected item in the combo box changed.
KTextEditor::Editor
Accessor interface for Editor part.
Definition: editor.h:103
QList< Ptr >
QWidget
editorchooser.h
kconfig.h
kconfiggroup.h
kglobal.h
klocale.h
i18n
QString i18n(const char *text)
kmimetypetrader.h
config
KSharedConfigPtr config()
KTextEditor
Namespace for the KDE Text Editor Interfaces.
Definition: annotationinterface.h:31
KTextEditor::editor
Editor * editor(const char *libname)
Helper function for the EditorChooser.
Definition: ktexteditor.cpp:173
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal