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

kapptemplate

  • sources
  • kde-4.12
  • kdesdk
  • kapptemplate
  • templates
  • C++
  • kpartapp
  • src
%{APPNAMELC}_part.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) %{CURRENT_YEAR} by %{AUTHOR} <%{EMAIL}> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * This program 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 *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18  ***************************************************************************/
19 
20 #include "%{APPNAMELC}_part.h"
21 
22 #include "%{APPNAMELC}_part.moc"
23 
24 #include <kaboutdata.h>
25 #include <kaction.h>
26 #include <kactioncollection.h>
27 #include <kcomponentdata.h>
28 #include <kfiledialog.h>
29 #include <kpluginfactory.h>
30 #include <kstandardaction.h>
31 
32 #include <QtCore/QFile>
33 #include <QtCore/QTextStream>
34 #include <QtGui/QTextEdit>
35 
36 K_PLUGIN_FACTORY(%{APPNAME}PartFactory, registerPlugin<%{APPNAME}Part>();)
37 K_EXPORT_PLUGIN(%{APPNAME}PartFactory)
38 
39 %{APPNAME}Part::%{APPNAME}Part(QWidget *parentWidget, QObject *parent, const QVariantList & /*args*/)
40  : KParts::ReadWritePart(parent)
41 {
42  // we need a component data
43  setComponentData(%{APPNAME}PartFactory::componentData());
44 
45  // this should be your custom internal widget
46  m_widget = new QTextEdit( parentWidget);
47 
48  // notify the part that this is our internal widget
49  setWidget(m_widget);
50 
51  // create our actions
52  KStandardAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
53  save = KStandardAction::save(this, SLOT(save()), actionCollection());
54 
55  // set our XML-UI resource file
56  setXMLFile("%{APPNAMELC}_part.rc");
57 
58  // we are read-write by default
59  setReadWrite(true);
60 
61  // we are not modified since we haven't done anything yet
62  setModified(false);
63 }
64 
65 %{APPNAME}Part::~%{APPNAME}Part()
66 {
67 }
68 
69 void %{APPNAME}Part::setReadWrite(bool rw)
70 {
71  // notify your internal widget of the read-write state
72  m_widget->setReadOnly(!rw);
73  if (rw)
74  connect(m_widget, SIGNAL(textChanged()),
75  this, SLOT(setModified()));
76  else
77  {
78  disconnect(m_widget, SIGNAL(textChanged()),
79  this, SLOT(setModified()));
80  }
81 
82  ReadWritePart::setReadWrite(rw);
83 }
84 
85 void %{APPNAME}Part::setModified(bool modified)
86 {
87  // get a handle on our Save action and make sure it is valid
88  if (!save)
89  return;
90 
91  // if so, we either enable or disable it based on the current
92  // state
93  if (modified)
94  save->setEnabled(true);
95  else
96  save->setEnabled(false);
97 
98  // in any event, we want our parent to do it's thing
99  ReadWritePart::setModified(modified);
100 }
101 
102 KAboutData *%{APPNAME}Part::createAboutData()
103 {
104  // the non-i18n name here must be the same as the directory in
105  // which the part's rc file is installed ('partrcdir' in the
106  // Makefile)
107  KAboutData *aboutData = new KAboutData("%{APPNAMELC}part", "%{APPNAMELC}", ki18n("%{APPNAME}Part"), "%{VERSION}");
108  aboutData->addAuthor(ki18n("%{AUTHOR}"), KLocalizedString(), "%{EMAIL}");
109  return aboutData;
110 }
111 
112 bool %{APPNAME}Part::openFile()
113 {
114  // m_file is always local so we can use QFile on it
115  QFile file("m_file");
116  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
117  return false;
118 
119  // our example widget is text-based, so we use QTextStream instead
120  // of a raw QDataStream
121  QTextStream stream(&file);
122  QString str;
123  while (!stream.atEnd())
124  str += stream.readLine() + "\n";
125 
126  file.close();
127 
128  // now that we have the entire file, display it
129  m_widget->setPlainText(str);
130 
131  // just for fun, set the status bar
132  //emit setStatusBarText( m_url.prettyUrl() );
133 
134  return true;
135 }
136 
137 bool %{APPNAME}Part::saveFile()
138 {
139  // if we aren't read-write, return immediately
140  if (isReadWrite() == false)
141  return false;
142 
143  // m_file is always local, so we use QFile
144  QFile file("m_file");
145  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
146  return false;
147 
148  // use QTextStream to dump the text to the file
149  QTextStream stream(&file);
150  stream << m_widget->document();
151 
152  file.close();
153 
154  return true;
155 }
156 
157 void %{APPNAME}Part::fileSaveAs()
158 {
159  // this slot is called whenever the File->Save As menu is selected,
160  QString file_name = KFileDialog::getSaveFileName();
161  if (file_name.isEmpty() == false)
162  saveAs(file_name);
163 }
164 
setReadWrite
virtual virtual void setReadWrite(bool rw)
Destructor.
setModified
virtual void setModified(bool modified)
Reimplemented to disable and enable Save action.
QWidget
class @6 QWidget
This is the main view class for %{APPNAME}.
createAboutData
static KAboutData * createAboutData()
openFile
virtual bool openFile()
This must be implemented by each part.
Part
class @11 Part(QWidget *parentWidget, QObject *parent, const QVariantList &)
This is a "Part".
m_widget
QTextEdit * m_widget
Definition: %{APPNAMELC}_part.h:84
APPNAME
main APPNAME
saveFile
virtual bool saveFile()
This must be implemented by each read-write part.
save
KAction * save
Definition: %{APPNAMELC}_part.h:78
fileSaveAs
void fileSaveAs()
%{APPNAMELC}_part.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kapptemplate

Skip menu "kapptemplate"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

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