1
2 keditor.h: Class Declaration for KEditor
3 #ifndef __KEDITOR_H__
4 #define __KEDITOR_H__
5
6 #include <ktmainwindow.h>
7 #include <kurl.h>
8
9 class QMultiLineEdit;
10
11 class KEditor : public KTMainWindow
12 {
13 Q_OBJECT
14 public:
15 KEditor (const char *name=0);
16
17 protected slots:
18 /**
19 * Update the line number field in the statusbar.
20 **/
21 void slotUpdateStatusBar ();
22 /**
23 * Open the "Save As" dialog.
24 **/
25 void slotSaveAs();
26 /**
27 * Save the file.
28 **/
29 void slotSave();
30 /**
31 * Open the "Open" dialog.
32 **/
33 void slotOpen();
34
35 private:
36 QMultiLineEdit *qmle;
37 KURL url;
38 QString file;
39 };
40
41 #endif
42 keditor.cpp: Class Definition for KEditor,
43 #include <qmultilineedit.h>
44
45 #include <kapp.h>
46 #include <kiconloader.h>
47 #include <kmenubar.h>
48 #include <kstdaction.h>
49 #include <kaction.h>
50
51 #include <netaccess.h>
52 #include <ktempfile.h>
53
54 #include "keditor.moc"
55
56 //Status Bar id
57 const int SBLineNumber = 2;
58
59 KEditor::KEditor (const char *name) : KTMainWindow (name)
60 {
61 qmle = new QMultiLineEdit (this);
62
63 KStdAction::openNew (qmle, SLOT (clear()), actionCollection());
64 KStdAction::quit (kapp, SLOT (closeAllWindows()), actionCollection());
65 KStdAction::copy (qmle, SLOT (copy()), actionCollection());
66 KStdAction::cut (qmle, SLOT (cut()), actionCollection());
67 KStdAction::paste (qmle, SLOT (paste()), actionCollection());
68 KStdAction::undo (qmle, SLOT (undo()), actionCollection());
69 KStdAction::redo (qmle, SLOT (redo()), actionCollection());
70
71 KStdAction::open(this, SLOT(slotOpen()), actionCollection());
72 KStdAction::save(this, SLOT(slotSave()), actionCollection());
73 KStdAction::saveAs(this, SLOT(slotSaveAs()), actionCollection());
74
75 createGUI();
76
77 statusBar()->insertItem ("Line", 1);
78 statusBar()->insertItem ("0000", SBLineNumber);
79 slotUpdateStatusBar();
80 connect ( qmle, SIGNAL (textChanged()),
81 this, SLOT (slotUpdateStatusBar()) );
82
83
84 setView (qmle);
85 }
86
87 void
88 KEditor::slotUpdateStatusBar ()
89 {
90 QString linenumber;
91 int line, col;
92
93 qmle->getCursorPosition (&line, &col);
94 linenumber.sprintf ("%4d", line);
95
96 statusBar()->changeItem (linenumber, SBLineNumber);
97 }
98
99 void
100 KEditor::slotSaveAs()
101 {
102 url=KFileDialog::getSaveUrl(0,
103 "*.txt|Text Files (*.txt)",this)
104 file=url.path();
105
106 if (!file.isLocalPath())
107 {
108 KTempFile temp;
109 file=temp.name();
110
111 slotSave();
112 temp.unlink();
113 return;
114 }
115 slotSave();
116 }
117
118 void
119 KEditor::slotSave()
120 {
121 if (url.isEmpty() || file.isEmpty())
122 slotSaveAs(), return;
123 QFile f(file);
124
125 if (!f.open(IO_WriteOnly | IO_Truncate))
126 KNotifyClient::event("cannotopenfile"), return;
127
128 QTextStream t( &f );
129 t << qmle->text();
130
131 f.close();
132 qmle->setEdited(false);
133 }
134
135 void
136 KEditor::slotOpen()
137 {
138 if ( qmle->edited() )
139 {
140 int result=KMessageBox::questionYesNo(this,
141 i18n("You already have a file open! Would you like
142 "to save the currently "
143 "opened file and open another?"),
144 i18n("Continue?"));
145
146 if (result==KMessageBox::Yes)
147 slotSave();
148 else
149 return;
150 }
151
152 url=KFileDialog::getOpenURL(0,
153 "*.txt|Text Files (*.txt)", this);
154
155 if (!KIO::NetAccess::download(url, file))
156 KNotifyClient::event("cannotopenfile"), return;
157
158 QFile f(file);
159 if (!f.open(IO_ReadOnly))
160 KNotifyClient::event("cannotopenfile"), return;
161
162 QTextStream t( &f );
163 QString text(t.read());
164 qmle->clear();
165 qmle->setText(text);
166 f.close();
167 }
168
169 // main.cpp: main() which can be used to test KEditor
170 #include <kapp.h>
171
172 #include "keditor.h"
173
174 int
175 main (int argc, char *argv[])
176 {
177 KApplication kapplication (argc, argv, "keditor");
178 KEditor *keditor = new KEditor (0);
179
180 kapplication.setMainWidget (keditor);
181
182 keditor->show();
183 return kapplication.exec();
184 }
185 |