3
#include "%{APPNAMELC}_part.h"
5
#include <kparts/genericfactory.h>
8
#include <kstdaction.h>
9
#include <kfiledialog.h>
14
#include <qtextstream.h>
15
#include <qmultilineedit.h>
17
typedef KParts::GenericFactory<%{APPNAMELC}Part> %{APPNAMELC}PartFactory;
18
K_EXPORT_COMPONENT_FACTORY( lib%{APPNAMELC}part, %{APPNAMELC}PartFactory );
20
%{APPNAMELC}Part::%{APPNAMELC}Part( QWidget *parentWidget, const char *widgetName,
21
QObject *parent, const char *name, const QStringList & /*args*/)
22
: KParts::ReadWritePart(parent)
24
// we need an instance
25
setInstance( %{APPNAMELC}PartFactory::instance() );
27
// this should be your custom internal widget
28
m_widget = new QMultiLineEdit( parentWidget, widgetName );
30
// notify the part that this is our internal widget
34
KStdAction::open(this, SLOT(fileOpen()), actionCollection());
35
KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
36
KStdAction::save(this, SLOT(save()), actionCollection());
38
// set our XML-UI resource file
39
setXMLFile("%{APPNAMELC}_part.rc");
41
// we are read-write by default
44
// we are not modified since we haven't done anything yet
48
%{APPNAMELC}Part::~%{APPNAMELC}Part()
52
KAboutData *%{APPNAMELC}Part::createAboutData()
54
// The non-i18n name here must be the same as the directory in
55
// which the part's rc file is installed
56
KAboutData *aboutData = new KAboutData("%{APPNAMELC}part", I18N_NOOP("%{APPNAMELC}Part"), "0.0.1");
57
aboutData->addAuthor("%{AUTHOR}", 0, "%{EMAIL}");
61
void %{APPNAMELC}Part::setReadWrite(bool rw)
63
// notify your internal widget of the read-write state
64
m_widget->setReadOnly(!rw);
66
connect(m_widget, SIGNAL(textChanged()),
67
this, SLOT(setModified()));
70
disconnect(m_widget, SIGNAL(textChanged()),
71
this, SLOT(setModified()));
74
ReadWritePart::setReadWrite(rw);
77
void %{APPNAMELC}Part::setModified(bool modified)
79
// get a handle on our Save action and make sure it is valid
80
KAction *save = actionCollection()->action(KStdAction::stdName(KStdAction::Save));
84
// if so, we either enable or disable it based on the current
87
save->setEnabled(true);
89
save->setEnabled(false);
91
// in any event, we want our parent to do it's thing
92
ReadWritePart::setModified(modified);
95
bool %{APPNAMELC}Part::openFile()
97
// m_file is always local so we can use QFile on it
99
if (file.open(IO_ReadOnly) == false)
102
// our example widget is text-based, so we use QTextStream instead
103
// of a raw QDataStream
104
QTextStream stream(&file);
106
while (!stream.eof())
107
str += stream.readLine() + "\n";
111
// now that we have the entire file, display it
112
m_widget->setText(str);
114
// just for fun, set the status bar
115
emit setStatusBarText( m_url.prettyURL() );
120
bool %{APPNAMELC}Part::saveFile()
122
// if we aren't read-write, return immediately
123
if (isReadWrite() == false)
126
// m_file is always local, so we use QFile
128
if (file.open(IO_WriteOnly) == false)
131
// use QTextStream to dump the text to the file
132
QTextStream stream(&file);
133
stream << m_widget->text();
140
void %{APPNAMELC}Part::fileOpen()
142
// this slot is called whenever the File->Open menu is selected,
143
// the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
145
QString file_name = KFileDialog::getOpenFileName();
147
if (file_name.isEmpty() == false)
151
void %{APPNAMELC}Part::fileSaveAs()
153
// this slot is called whenever the File->Save As menu is selected,
154
QString file_name = KFileDialog::getSaveFileName();
155
if (file_name.isEmpty() == false)
159
#include "%{APPNAMELC}_part.moc"