~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/app_templates/kscons_kmdi/app_part.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

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