summaryrefslogtreecommitdiffstats
path: root/languages/cpp/app_templates/kpartapp/app_part.cpp
blob: 45a8116ad8f187b4bd472a0e242a44e3b39eda2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

#include "%{APPNAMELC}_part.h"

#include <kinstance.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kfiledialog.h>
#include <kglobal.h>
#include <klocale.h>

#include <tqfile.h>
#include <tqtextstream.h>
#include <tqmultilineedit.h>

%{APPNAME}Part::%{APPNAME}Part( TQWidget *tqparentWidget, const char *widgetName,
                                  TQObject *tqparent, const char *name )
    : KParts::ReadWritePart(tqparent, name)
{
    // we need an instance
    setInstance( %{APPNAME}PartFactory::instance() );

    // this should be your custom internal widget
    m_widget = new TQMultiLineEdit( tqparentWidget, widgetName );

    // notify the part that this is our internal widget
    setWidget(m_widget);

    // create our actions
    KStdAction::open(this, TQT_SLOT(fileOpen()), actionCollection());
    KStdAction::saveAs(this, TQT_SLOT(fileSaveAs()), actionCollection());
    KStdAction::save(this, TQT_SLOT(save()), actionCollection());

    // set our XML-UI resource file
    setXMLFile("%{APPNAMELC}_part.rc");

    // we are read-write by default
    setReadWrite(true);

    // we are not modified since we haven't done anything yet
    setModified(false);
}

%{APPNAME}Part::~%{APPNAME}Part()
{
}

void %{APPNAME}Part::setReadWrite(bool rw)
{
    // notify your internal widget of the read-write state
    m_widget->setReadOnly(!rw);
    if (rw)
        connect(m_widget, TQT_SIGNAL(textChanged()),
                this,     TQT_SLOT(setModified()));
    else
    {
        disconnect(m_widget, TQT_SIGNAL(textChanged()),
                   this,     TQT_SLOT(setModified()));
    }

    ReadWritePart::setReadWrite(rw);
}

void %{APPNAME}Part::setModified(bool modified)
{
    // get a handle on our Save action and make sure it is valid
    KAction *save = actionCollection()->action(KStdAction::stdName(KStdAction::Save));
    if (!save)
        return;

    // if so, we either enable or disable it based on the current
    // state
    if (modified)
        save->setEnabled(true);
    else
        save->setEnabled(false);

    // in any event, we want our tqparent to do it's thing
    ReadWritePart::setModified(modified);
}

bool %{APPNAME}Part::openFile()
{
    // m_file is always local so we can use TQFile on it
    TQFile file(m_file);
    if (file.open(IO_ReadOnly) == false)
        return false;

    // our example widget is text-based, so we use TQTextStream instead
    // of a raw TQDataStream
    TQTextStream stream(&file);
    TQString str;
    while (!stream.eof())
        str += stream.readLine() + "\n";

    file.close();

    // now that we have the entire file, display it
    m_widget->setText(str);

    // just for fun, set the status bar
    emit setStatusBarText( m_url.prettyURL() );

    return true;
}

bool %{APPNAME}Part::saveFile()
{
    // if we aren't read-write, return immediately
    if (isReadWrite() == false)
        return false;

    // m_file is always local, so we use TQFile
    TQFile file(m_file);
    if (file.open(IO_WriteOnly) == false)
        return false;

    // use TQTextStream to dump the text to the file
    TQTextStream stream(&file);
    stream << m_widget->text();

    file.close();

    return true;
}

void %{APPNAME}Part::fileOpen()
{
    // this slot is called whenever the File->Open menu is selected,
    // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
    // button is clicked
    TQString file_name = KFileDialog::getOpenFileName();

    if (file_name.isEmpty() == false)
        openURL(file_name);
}

void %{APPNAME}Part::fileSaveAs()
{
    // this slot is called whenever the File->Save As menu is selected,
    TQString file_name = KFileDialog::getSaveFileName();
    if (file_name.isEmpty() == false)
        saveAs(file_name);
}


// It's usually safe to leave the factory code alone.. with the
// notable exception of the KAboutData data
#include <kaboutdata.h>
#include <klocale.h>

KInstance*  %{APPNAME}PartFactory::s_instance = 0L;
KAboutData* %{APPNAME}PartFactory::s_about = 0L;

%{APPNAME}PartFactory::%{APPNAME}PartFactory()
    : KParts::Factory()
{
}

%{APPNAME}PartFactory::~%{APPNAME}PartFactory()
{
    delete s_instance;
    delete s_about;

    s_instance = 0L;
}

KParts::Part* %{APPNAME}PartFactory::createPartObject( TQWidget *tqparentWidget, const char *widgetName,
                                                        TQObject *tqparent, const char *name,
                                                        const char *classname, const TQStringList &args )
{
    // Create an instance of our Part
    %{APPNAME}Part* obj = new %{APPNAME}Part( tqparentWidget, widgetName, tqparent, name );

    // See if we are to be read-write or not
    if (TQCString(classname) == "KParts::ReadOnlyPart")
        obj->setReadWrite(false);

    return obj;
}

KInstance* %{APPNAME}PartFactory::instance()
{
    if( !s_instance )
    {
        s_about = new KAboutData("%{APPNAMELC}part", I18N_NOOP("%{APPNAME}Part"), "%{VERSION}");
        s_about->addAuthor("%{AUTHOR}", 0, "%{EMAIL}");
        s_instance = new KInstance(s_about);
    }
    return s_instance;
}

extern "C"
{
    void* init_lib%{APPNAMELC}part()
    {
	KGlobal::locale()->insertCatalogue("%{APPNAMELC}");
        return new %{APPNAME}PartFactory;
    }
};

#include "%{APPNAMELC}_part.moc"