~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to kapptemplate/templates/C++/kpartapp/src/%{APPNAMELC}_part.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "%{APPNAMELC}_part.h"
 
2
 
 
3
#include "%{APPNAMELC}_part.moc"
 
4
 
 
5
#include <kaction.h>
 
6
#include <kactioncollection.h>
 
7
#include <kcomponentdata.h>
 
8
#include <kfiledialog.h>
 
9
#include <kparts/genericfactory.h>
 
10
#include <kstandardaction.h>
 
11
 
 
12
#include <QtCore/QFile>
 
13
#include <QtCore/QTextStream>
 
14
#include <QtGui/QTextEdit>
 
15
 
 
16
typedef KParts::GenericFactory<%{APPNAME}Part> %{APPNAME}PartFactory;
 
17
K_EXPORT_COMPONENT_FACTORY( lib%{APPNAMELC}part, %{APPNAME}PartFactory )
 
18
 
 
19
%{APPNAME}Part::%{APPNAME}Part( QWidget *parentWidget, QObject *parent, const QStringList & /*args*/ )
 
20
    : KParts::ReadWritePart(parent)
 
21
{
 
22
    // we need an instance
 
23
    setComponentData( %{APPNAME}PartFactory::componentData() );
 
24
 
 
25
    // this should be your custom internal widget
 
26
    m_widget = new QTextEdit( parentWidget);
 
27
 
 
28
    // notify the part that this is our internal widget
 
29
    setWidget(m_widget);
 
30
 
 
31
    // create our actions
 
32
    KStandardAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
 
33
    save = KStandardAction::save(this, SLOT(save()), actionCollection());
 
34
 
 
35
    // set our XML-UI resource file
 
36
    setXMLFile("%{APPNAMELC}_part.rc");
 
37
 
 
38
    // we are read-write by default
 
39
    setReadWrite(true);
 
40
 
 
41
    // we are not modified since we haven't done anything yet
 
42
    setModified(false);
 
43
}
 
44
 
 
45
%{APPNAME}Part::~%{APPNAME}Part()
 
46
{
 
47
}
 
48
 
 
49
void %{APPNAME}Part::setReadWrite(bool rw)
 
50
{
 
51
    // notify your internal widget of the read-write state
 
52
    m_widget->setReadOnly(!rw);
 
53
    if (rw)
 
54
        connect(m_widget, SIGNAL(textChanged()),
 
55
                this,     SLOT(setModified()));
 
56
    else
 
57
    {
 
58
        disconnect(m_widget, SIGNAL(textChanged()),
 
59
                   this,     SLOT(setModified()));
 
60
    }
 
61
 
 
62
    ReadWritePart::setReadWrite(rw);
 
63
}
 
64
 
 
65
void %{APPNAME}Part::setModified(bool modified)
 
66
{
 
67
    // get a handle on our Save action and make sure it is valid
 
68
    if (!save)
 
69
        return;
 
70
 
 
71
    // if so, we either enable or disable it based on the current
 
72
    // state
 
73
    if (modified)
 
74
        save->setEnabled(true);
 
75
    else
 
76
        save->setEnabled(false);
 
77
 
 
78
    // in any event, we want our parent to do it's thing
 
79
    ReadWritePart::setModified(modified);
 
80
}
 
81
 
 
82
KAboutData *%{APPNAME}Part::createAboutData()
 
83
{
 
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
 
86
    // Makefile)
 
87
    KAboutData *aboutData = new KAboutData("%{APPNAMELC}part", 0, ki18n("%{APPNAME}Part"), "%{VERSION}");
 
88
    aboutData->addAuthor(ki18n("%{AUTHOR}"), KLocalizedString(), "%{EMAIL}");
 
89
    return aboutData;
 
90
}
 
91
 
 
92
bool %{APPNAME}Part::openFile()
 
93
{
 
94
    // m_file is always local so we can use QFile on it
 
95
    QFile file("m_file");
 
96
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
 
97
        return false;
 
98
 
 
99
    // our example widget is text-based, so we use QTextStream instead
 
100
    // of a raw QDataStream
 
101
    QTextStream stream(&file);
 
102
    QString str;
 
103
    while (!stream.atEnd())
 
104
        str += stream.readLine() + "\n";
 
105
 
 
106
    file.close();
 
107
 
 
108
    // now that we have the entire file, display it
 
109
    m_widget->setPlainText(str);
 
110
 
 
111
    // just for fun, set the status bar
 
112
    //emit setStatusBarText( m_url.prettyUrl() );
 
113
 
 
114
    return true;
 
115
}
 
116
 
 
117
bool %{APPNAME}Part::saveFile()
 
118
{
 
119
    // if we aren't read-write, return immediately
 
120
    if (isReadWrite() == false)
 
121
        return false;
 
122
 
 
123
    // m_file is always local, so we use QFile
 
124
    QFile file("m_file");
 
125
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
 
126
        return false;
 
127
 
 
128
    // use QTextStream to dump the text to the file
 
129
    QTextStream stream(&file);
 
130
    stream << m_widget->document();
 
131
 
 
132
    file.close();
 
133
 
 
134
    return true;
 
135
}
 
136
 
 
137
void %{APPNAME}Part::fileSaveAs()
 
138
{
 
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)
 
142
        saveAs(file_name);
 
143
}
 
144