1
#include "%{APPNAMELC}_part.h"
3
#include "%{APPNAMELC}_part.moc"
6
#include <kactioncollection.h>
7
#include <kcomponentdata.h>
8
#include <kfiledialog.h>
9
#include <kparts/genericfactory.h>
10
#include <kstandardaction.h>
12
#include <QtCore/QFile>
13
#include <QtCore/QTextStream>
14
#include <QtGui/QTextEdit>
16
typedef KParts::GenericFactory<%{APPNAME}Part> %{APPNAME}PartFactory;
17
K_EXPORT_COMPONENT_FACTORY( lib%{APPNAMELC}part, %{APPNAME}PartFactory )
19
%{APPNAME}Part::%{APPNAME}Part( QWidget *parentWidget, QObject *parent, const QStringList & /*args*/ )
20
: KParts::ReadWritePart(parent)
22
// we need an instance
23
setComponentData( %{APPNAME}PartFactory::componentData() );
25
// this should be your custom internal widget
26
m_widget = new QTextEdit( parentWidget);
28
// notify the part that this is our internal widget
32
KStandardAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
33
save = KStandardAction::save(this, SLOT(save()), actionCollection());
35
// set our XML-UI resource file
36
setXMLFile("%{APPNAMELC}_part.rc");
38
// we are read-write by default
41
// we are not modified since we haven't done anything yet
45
%{APPNAME}Part::~%{APPNAME}Part()
49
void %{APPNAME}Part::setReadWrite(bool rw)
51
// notify your internal widget of the read-write state
52
m_widget->setReadOnly(!rw);
54
connect(m_widget, SIGNAL(textChanged()),
55
this, SLOT(setModified()));
58
disconnect(m_widget, SIGNAL(textChanged()),
59
this, SLOT(setModified()));
62
ReadWritePart::setReadWrite(rw);
65
void %{APPNAME}Part::setModified(bool modified)
67
// get a handle on our Save action and make sure it is valid
71
// if so, we either enable or disable it based on the current
74
save->setEnabled(true);
76
save->setEnabled(false);
78
// in any event, we want our parent to do it's thing
79
ReadWritePart::setModified(modified);
82
KAboutData *%{APPNAME}Part::createAboutData()
84
// the non-i18n name here must be the same as the directory in
85
// which the part's rc file is installed ('partrcdir' in the
87
KAboutData *aboutData = new KAboutData("%{APPNAMELC}part", 0, ki18n("%{APPNAME}Part"), "%{VERSION}");
88
aboutData->addAuthor(ki18n("%{AUTHOR}"), KLocalizedString(), "%{EMAIL}");
92
bool %{APPNAME}Part::openFile()
94
// m_file is always local so we can use QFile on it
96
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
99
// our example widget is text-based, so we use QTextStream instead
100
// of a raw QDataStream
101
QTextStream stream(&file);
103
while (!stream.atEnd())
104
str += stream.readLine() + "\n";
108
// now that we have the entire file, display it
109
m_widget->setPlainText(str);
111
// just for fun, set the status bar
112
//emit setStatusBarText( m_url.prettyUrl() );
117
bool %{APPNAME}Part::saveFile()
119
// if we aren't read-write, return immediately
120
if (isReadWrite() == false)
123
// m_file is always local, so we use QFile
124
QFile file("m_file");
125
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
128
// use QTextStream to dump the text to the file
129
QTextStream stream(&file);
130
stream << m_widget->document();
137
void %{APPNAME}Part::fileSaveAs()
139
// this slot is called whenever the File->Save As menu is selected,
140
QString file_name = KFileDialog::getSaveFileName();
141
if (file_name.isEmpty() == false)