• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • mode
katemodeconfigpage.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2001-2010 Christoph Cullmann <cullmann@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 //BEGIN Includes
22 #include "katemodeconfigpage.h"
23 #include "katemodeconfigpage.moc"
24 
25 #include "katedocument.h"
26 #include "kateconfig.h"
27 #include "kateview.h"
28 #include "kateglobal.h"
29 #include "kateautoindent.h"
30 #include "katesyntaxmanager.h"
31 #include "katesyntaxdocument.h"
32 
33 #include "ui_filetypeconfigwidget.h"
34 
35 #include <kconfig.h>
36 #include <kmimetype.h>
37 #include <kmimetypechooser.h>
38 #include <kdebug.h>
39 #include <kicon.h>
40 #include <knuminput.h>
41 #include <klocale.h>
42 #include <kmenu.h>
43 
44 #include <QtCore/QRegExp>
45 #include <QtGui/QCheckBox>
46 #include <QtGui/QComboBox>
47 #include <QtGui/QGroupBox>
48 
49 #include <QtGui/QLabel>
50 #include <QtGui/QLayout>
51 #include <QtGui/QPushButton>
52 #include <QtGui/QToolButton>
53 #include <kvbox.h>
54 
55 #define KATE_FT_HOWMANY 1024
56 //END Includes
57 
58 ModeConfigPage::ModeConfigPage( QWidget *parent )
59  : KateConfigPage( parent )
60 {
61  m_lastType = -1;
62 
63  // This will let us have more separation between this page and
64  // the KTabWidget edge (ereslibre)
65  QVBoxLayout *layout = new QVBoxLayout;
66  QWidget *newWidget = new QWidget(this);
67 
68  ui = new Ui::FileTypeConfigWidget();
69  ui->setupUi( newWidget );
70 
71  ui->cmbHl->addItem(i18n("<Unchanged>"), QVariant(""));
72  for( int i = 0; i < KateHlManager::self()->highlights(); i++) {
73  if (KateHlManager::self()->hlSection(i).length() > 0)
74  ui->cmbHl->addItem(KateHlManager::self()->hlSection(i) + QString ("/")
75  + KateHlManager::self()->hlNameTranslated(i), QVariant(KateHlManager::self()->hlName(i)));
76  else
77  ui->cmbHl->addItem(KateHlManager::self()->hlNameTranslated(i), QVariant(KateHlManager::self()->hlName(i)));
78  }
79 
80  QStringList indentationModes;
81  indentationModes << i18n ("Use Default");
82  indentationModes << KateAutoIndent::listModes();
83  ui->cmbIndenter->addItems (indentationModes);
84 
85  connect( ui->cmbFiletypes, SIGNAL(activated(int)), this, SLOT(typeChanged(int)) );
86  connect( ui->btnNew, SIGNAL(clicked()), this, SLOT(newType()) );
87  connect( ui->btnDelete, SIGNAL(clicked()), this, SLOT(deleteType()) );
88  ui->btnMimeTypes->setIcon(KIcon("tools-wizard"));
89  connect(ui->btnMimeTypes, SIGNAL(clicked()), this, SLOT(showMTDlg()));
90  connect( ui->btnDownload, SIGNAL(clicked()), this, SLOT(hlDownload()) );
91 
92  reload();
93 
94  connect( ui->edtName, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()) );
95  connect( ui->edtSection, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()) );
96  connect( ui->edtVariables, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()) );
97  connect( ui->edtFileExtensions, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()) );
98  connect( ui->edtMimeTypes, SIGNAL(textChanged(QString)), this, SLOT(slotChanged()) );
99  connect( ui->sbPriority, SIGNAL(valueChanged(int)), this, SLOT(slotChanged()) );
100  connect( ui->cmbHl, SIGNAL(activated(int)), this, SLOT(slotChanged()) );
101  connect( ui->cmbIndenter, SIGNAL(activated(int)), this, SLOT(slotChanged()) );
102 
103  layout->addWidget(newWidget);
104  setLayout(layout);
105 }
106 
107 ModeConfigPage::~ModeConfigPage ()
108 {
109  qDeleteAll (m_types);
110  delete ui;
111 }
112 
113 void ModeConfigPage::apply()
114 {
115  if (!hasChanged())
116  return;
117 
118  save ();
119 
120  KateGlobal::self()->modeManager()->save(m_types);
121 }
122 
123 void ModeConfigPage::reload()
124 {
125  qDeleteAll (m_types);
126  m_types.clear();
127 
128  // deep copy...
129  foreach (KateFileType *type, KateGlobal::self()->modeManager()->list())
130  {
131  KateFileType *t = new KateFileType ();
132  *t = *type;
133  m_types.append (t);
134  }
135 
136  update ();
137 }
138 
139 void ModeConfigPage::reset()
140 {
141  reload ();
142 }
143 
144 void ModeConfigPage::defaults()
145 {
146  reload ();
147 }
148 
149 void ModeConfigPage::update ()
150 {
151  m_lastType = -1;
152 
153  ui->cmbFiletypes->clear ();
154 
155  foreach (KateFileType *type, m_types) {
156  if (!type->sectionTranslated().isEmpty())
157  ui->cmbFiletypes->addItem(type->sectionTranslated() + QString ("/") + type->nameTranslated());
158  else
159  ui->cmbFiletypes->addItem(type->nameTranslated());
160  }
161 
162  // get current filetype from active view via the host application
163  int currentIndex = 0;
164  KTextEditor::MdiContainer *iface = qobject_cast<KTextEditor::MdiContainer*>(KateGlobal::self()->container());
165  if (iface) {
166  KateView *kv = qobject_cast<KateView*>(iface->activeView());
167  if (kv) {
168  const QString filetypeName = kv->doc()->fileType();
169  for (int i = 0; i < m_types.size(); ++i) {
170  if (filetypeName == m_types[i]->name) {
171  currentIndex = i;
172  break;
173  }
174  }
175  }
176  }
177  ui->cmbFiletypes->setCurrentIndex (currentIndex);
178  typeChanged (currentIndex);
179 
180  ui->cmbFiletypes->setEnabled (ui->cmbFiletypes->count() > 0);
181 }
182 
183 void ModeConfigPage::deleteType ()
184 {
185  int type = ui->cmbFiletypes->currentIndex ();
186 
187  if (type > -1 && type < m_types.count())
188  {
189  delete m_types[type];
190  m_types.removeAt(type);
191  update ();
192  }
193 }
194 
195 void ModeConfigPage::newType ()
196 {
197  QString newN = i18n("New Filetype");
198 
199  for (int i = 0; i < m_types.count(); ++i) {
200  KateFileType *type = m_types.at(i);
201  if (type->name == newN)
202  {
203  ui->cmbFiletypes->setCurrentIndex (i);
204  typeChanged (i);
205  return;
206  }
207  }
208 
209  KateFileType *newT = new KateFileType ();
210  newT->priority = 0;
211  newT->name = newN;
212  newT->hlGenerated = false;
213 
214  m_types.prepend (newT);
215 
216  update ();
217 }
218 
219 void ModeConfigPage::save ()
220 {
221  if (m_lastType != -1)
222  {
223  if (!m_types[m_lastType]->hlGenerated) {
224  m_types[m_lastType]->name = ui->edtName->text ();
225  m_types[m_lastType]->section = ui->edtSection->text ();
226  }
227  m_types[m_lastType]->varLine = ui->edtVariables->text ();
228  m_types[m_lastType]->wildcards = ui->edtFileExtensions->text().split (';', QString::SkipEmptyParts);
229  m_types[m_lastType]->mimetypes = ui->edtMimeTypes->text().split (';', QString::SkipEmptyParts);
230  m_types[m_lastType]->priority = ui->sbPriority->value();
231  m_types[m_lastType]->hl = ui->cmbHl->itemData(ui->cmbHl->currentIndex()).toString();
232 
233  if (ui->cmbIndenter->currentIndex() > 0)
234  m_types[m_lastType]->indenter = KateAutoIndent::modeName (ui->cmbIndenter->currentIndex() - 1);
235  else
236  m_types[m_lastType]->indenter = "";
237  }
238 }
239 
240 void ModeConfigPage::typeChanged (int type)
241 {
242  save ();
243 
244  ui->cmbHl->setEnabled (true);
245  ui->btnDelete->setEnabled (true);
246  ui->edtName->setEnabled (true);
247  ui->edtSection->setEnabled (true);
248 
249  if (type > -1 && type < m_types.count())
250  {
251  KateFileType *t = m_types.at(type);
252 
253  ui->gbProperties->setTitle (i18n("Properties of %1", ui->cmbFiletypes->currentText()));
254 
255  ui->gbProperties->setEnabled (true);
256  ui->btnDelete->setEnabled (true);
257 
258  ui->edtName->setText(t->nameTranslated());
259  ui->edtSection->setText(t->sectionTranslated());
260  ui->edtVariables->setText(t->varLine);
261  ui->edtFileExtensions->setText(t->wildcards.join (";"));
262  ui->edtMimeTypes->setText(t->mimetypes.join (";"));
263  ui->sbPriority->setValue(t->priority);
264 
265  ui->cmbHl->setEnabled (!t->hlGenerated);
266  ui->btnDelete->setEnabled (!t->hlGenerated);
267  ui->edtName->setEnabled (!t->hlGenerated);
268  ui->edtSection->setEnabled (!t->hlGenerated);
269 
270  // activate current hl...
271  for (int i = 0; i < ui->cmbHl->count(); ++i)
272  if (ui->cmbHl->itemData (i).toString() == t->hl)
273  ui->cmbHl->setCurrentIndex (i);
274 
275  // activate the right indenter
276  int indenterIndex = 0;
277  if (!t->indenter.isEmpty())
278  indenterIndex = KateAutoIndent::modeNumber (t->indenter) + 1;
279  ui->cmbIndenter->setCurrentIndex (indenterIndex);
280  }
281  else
282  {
283  ui->gbProperties->setTitle (i18n("Properties"));
284 
285  ui->gbProperties->setEnabled (false);
286  ui->btnDelete->setEnabled (false);
287 
288  ui->edtName->clear();
289  ui->edtSection->clear();
290  ui->edtVariables->clear();
291  ui->edtFileExtensions->clear();
292  ui->edtMimeTypes->clear();
293  ui->sbPriority->setValue(0);
294  ui->cmbHl->setCurrentIndex (0);
295  ui->cmbIndenter->setCurrentIndex (0);
296  }
297 
298  m_lastType = type;
299 }
300 
301 void ModeConfigPage::showMTDlg()
302 {
303  QString text = i18n("Select the MimeTypes you want for this file type.\nPlease note that this will automatically edit the associated file extensions as well.");
304  QStringList list = ui->edtMimeTypes->text().split( QRegExp("\\s*;\\s*"), QString::SkipEmptyParts );
305  KMimeTypeChooserDialog d( i18n("Select Mime Types"), text, list, "text", this );
306  if ( d.exec() == KDialog::Accepted ) {
307  // do some checking, warn user if mime types or patterns are removed.
308  // if the lists are empty, and the fields not, warn.
309  ui->edtFileExtensions->setText( d.chooser()->patterns().join(";") );
310  ui->edtMimeTypes->setText( d.chooser()->mimeTypes().join(";") );
311  }
312 }
313 
314 void ModeConfigPage::hlDownload()
315 {
316  KateHlDownloadDialog diag(this,"hlDownload",true);
317  diag.exec();
318 }
319 
320 // kate: space-indent on; indent-width 2; replace-tabs on;
QList::clear
void clear()
QWidget
kateview.h
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
KateGlobal::modeManager
KateModeManager * modeManager()
global mode manager used to manage the modes centrally
Definition: kateglobal.h:292
KateGlobal::container
QObject * container()
Get the currently associated Container object.
Definition: kateglobal.cpp:519
KateFileType::hl
QString hl
Definition: katemodemanager.h:44
kateautoindent.h
QList::at
const T & at(int i) const
QList::removeAt
void removeAt(int i)
katedocument.h
KateDocument::fileType
QString fileType() const
Definition: katedocument.h:939
KateModeManager::save
void save(const QList< KateFileType * > &v)
Definition: katemodemanager.cpp:162
KateFileType::nameTranslated
QString nameTranslated() const
Definition: katemodemanager.h:49
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
QStringList::join
QString join(const QString &separator) const
ModeConfigPage::reload
void reload()
Definition: katemodeconfigpage.cpp:123
KateConfigPage::slotChanged
void slotChanged()
Definition: katedialogs.cpp:135
KateHlManager::hlNameTranslated
QString hlNameTranslated(int n)
Definition: katesyntaxmanager.cpp:365
QList::size
int size() const
QRegExp
ModeConfigPage::ModeConfigPage
ModeConfigPage(QWidget *parent)
Definition: katemodeconfigpage.cpp:58
ModeConfigPage::defaults
void defaults()
Definition: katemodeconfigpage.cpp:144
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
katesyntaxmanager.h
KateFileType::wildcards
QStringList wildcards
Definition: katemodemanager.h:40
KateFileType::mimetypes
QStringList mimetypes
Definition: katemodemanager.h:41
kateglobal.h
QString::isEmpty
bool isEmpty() const
katesyntaxdocument.h
KateConfigPage::hasChanged
bool hasChanged()
Definition: katedialogs.h:103
QVBoxLayout
KateAutoIndent::modeNumber
static uint modeNumber(const QString &name)
Maps name -> index.
Definition: kateautoindent.cpp:102
KateFileType::indenter
QString indenter
Definition: katemodemanager.h:47
QString
QStringList
KateView
Definition: kateview.h:77
KateFileType::hlGenerated
bool hlGenerated
Definition: katemodemanager.h:45
KateFileType
Definition: katemodemanager.h:34
ModeConfigPage::~ModeConfigPage
~ModeConfigPage()
Definition: katemodeconfigpage.cpp:107
KateHlDownloadDialog
Definition: katedialogs.h:374
KateFileType::sectionTranslated
QString sectionTranslated() const
Definition: katemodemanager.h:55
QTest::toString
char * toString(const T &value)
KateAutoIndent::modeName
const QString & modeName() const
mode name
Definition: kateautoindent.h:177
KateHlManager::hlSection
QString hlSection(int n)
Definition: katesyntaxmanager.cpp:370
KateFileType::priority
int priority
Definition: katemodemanager.h:42
KateConfigPage
Definition: katedialogs.h:94
QString::length
int length() const
ModeConfigPage::apply
void apply()
Definition: katemodeconfigpage.cpp:113
katemodeconfigpage.h
QList::prepend
void prepend(const T &value)
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:553
KateFileType::varLine
QString varLine
Definition: katemodemanager.h:43
KateFileType::name
QString name
Definition: katemodemanager.h:38
ModeConfigPage::reset
void reset()
Definition: katemodeconfigpage.cpp:139
kateconfig.h
KateHlManager::self
static KateHlManager * self()
Definition: katesyntaxmanager.cpp:103
KateAutoIndent::listModes
static QStringList listModes()
List all possible modes by name, i.e.
Definition: kateautoindent.cpp:45
QVariant
KateHlManager::highlights
int highlights()
Definition: katesyntaxmanager.cpp:355
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



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