Changeset View
Changeset View
Standalone View
Standalone View
kwrite/kwrite.cpp
Show All 18 Lines | |||||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "kwrite.h" | 21 | #include "kwrite.h" | ||
22 | 22 | | |||
23 | #include <ktexteditor/document.h> | 23 | #include <ktexteditor/document.h> | ||
24 | #include <ktexteditor/view.h> | 24 | #include <ktexteditor/view.h> | ||
25 | #include <ktexteditor/modificationinterface.h> | 25 | #include <ktexteditor/modificationinterface.h> | ||
26 | #include <ktexteditor/editor.h> | 26 | #include <ktexteditor/editor.h> | ||
27 | #include <ktexteditor/application.h> | ||||
27 | 28 | | |||
28 | #include <KAboutApplicationDialog> | 29 | #include <KAboutApplicationDialog> | ||
29 | #include <KActionCollection> | 30 | #include <KActionCollection> | ||
30 | #include <KToggleAction> | 31 | #include <KToggleAction> | ||
31 | #include <KEditToolBar> | 32 | #include <KEditToolBar> | ||
32 | #include <KLocalizedString> | 33 | #include <KLocalizedString> | ||
33 | #include <KMessageBox> | 34 | #include <KMessageBox> | ||
34 | #include <KRecentFilesAction> | 35 | #include <KRecentFilesAction> | ||
Show All 16 Lines | |||||
51 | #include <QApplication> | 52 | #include <QApplication> | ||
52 | #include <QLabel> | 53 | #include <QLabel> | ||
53 | #include <QDragEnterEvent> | 54 | #include <QDragEnterEvent> | ||
54 | #include <QMenuBar> | 55 | #include <QMenuBar> | ||
55 | #include <QDir> | 56 | #include <QDir> | ||
56 | #include <QFileDialog> | 57 | #include <QFileDialog> | ||
57 | #include <QFileOpenEvent> | 58 | #include <QFileOpenEvent> | ||
58 | 59 | | |||
59 | QList<KTextEditor::Document *> KWrite::docList; | 60 | #include "kwriteapplication.h" | ||
60 | QList<KWrite *> KWrite::winList; | | |||
61 | 61 | | |||
62 | KWrite::KWrite(KTextEditor::Document *doc) | 62 | KWrite::KWrite(KTextEditor::Document *doc, KWriteApplication *app) | ||
63 | : m_view(nullptr) | 63 | : m_view(nullptr) | ||
64 | , m_recentFiles(nullptr) | 64 | , m_recentFiles(nullptr) | ||
65 | , m_paShowPath(nullptr) | 65 | , m_paShowPath(nullptr) | ||
66 | , m_paShowMenuBar(nullptr) | 66 | , m_paShowMenuBar(nullptr) | ||
67 | , m_paShowStatusBar(nullptr) | 67 | , m_paShowStatusBar(nullptr) | ||
68 | , m_activityResource(nullptr) | 68 | , m_activityResource(nullptr) | ||
69 | , m_app(app) | ||||
70 | , m_mainWindow(this) | ||||
69 | { | 71 | { | ||
70 | if (!doc) { | 72 | if (!doc) { | ||
71 | doc = KTextEditor::Editor::instance()->createDocument(nullptr); | 73 | doc = KTextEditor::Editor::instance()->createDocument(nullptr); | ||
72 | 74 | | |||
73 | // enable the modified on disk warning dialogs if any | 75 | // enable the modified on disk warning dialogs if any | ||
74 | if (qobject_cast<KTextEditor::ModificationInterface *>(doc)) { | 76 | if (qobject_cast<KTextEditor::ModificationInterface *>(doc)) { | ||
75 | qobject_cast<KTextEditor::ModificationInterface *>(doc)->setModifiedOnDiskWarning(true); | 77 | qobject_cast<KTextEditor::ModificationInterface *>(doc)->setModifiedOnDiskWarning(true); | ||
76 | } | 78 | } | ||
77 | 79 | | |||
78 | docList.append(doc); | 80 | m_app->addDocument(doc); | ||
79 | } | 81 | } | ||
80 | 82 | | |||
81 | m_view = doc->createView(this); | 83 | m_view = doc->createView(this); | ||
82 | 84 | | |||
83 | setCentralWidget(m_view); | 85 | setCentralWidget(m_view); | ||
84 | 86 | | |||
85 | setupActions(); | 87 | setupActions(); | ||
86 | 88 | | |||
Show All 13 Lines | |||||
100 | // FIXME: make sure the config dir exists, any idea how to do it more cleanly? | 102 | // FIXME: make sure the config dir exists, any idea how to do it more cleanly? | ||
101 | QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)).mkpath(QStringLiteral(".")); | 103 | QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)).mkpath(QStringLiteral(".")); | ||
102 | 104 | | |||
103 | // call it as last thing, must be sure everything is already set up ;) | 105 | // call it as last thing, must be sure everything is already set up ;) | ||
104 | setAutoSaveSettings(); | 106 | setAutoSaveSettings(); | ||
105 | 107 | | |||
106 | readConfig(); | 108 | readConfig(); | ||
107 | 109 | | |||
108 | winList.append(this); | | |||
109 | | ||||
110 | documentNameChanged(); | 110 | documentNameChanged(); | ||
111 | show(); | 111 | show(); | ||
112 | 112 | | |||
113 | // give view focus | 113 | // give view focus | ||
114 | m_view->setFocus(Qt::OtherFocusReason); | 114 | m_view->setFocus(Qt::OtherFocusReason); | ||
115 | 115 | | |||
116 | /** | 116 | /** | ||
117 | * handle mac os x like file open request via event filter | 117 | * handle mac os x like file open request via event filter | ||
118 | */ | 118 | */ | ||
119 | qApp->installEventFilter(this); | 119 | qApp->installEventFilter(this); | ||
120 | } | 120 | } | ||
121 | 121 | | |||
122 | KWrite::~KWrite() | 122 | KWrite::~KWrite() | ||
123 | { | 123 | { | ||
124 | m_app->removeWindow(this); | ||||
124 | guiFactory()->removeClient(m_view); | 125 | guiFactory()->removeClient(m_view); | ||
125 | 126 | | |||
126 | winList.removeAll(this); | | |||
127 | | ||||
128 | KTextEditor::Document *doc = m_view->document(); | 127 | KTextEditor::Document *doc = m_view->document(); | ||
129 | delete m_view; | 128 | delete m_view; | ||
130 | 129 | | |||
131 | // kill document, if last view is closed | 130 | // kill document, if last view is closed | ||
132 | if (doc->views().isEmpty()) { | 131 | if (doc->views().isEmpty()) { | ||
133 | docList.removeAll(doc); | 132 | m_app->removeDocument(doc); | ||
134 | delete doc; | 133 | delete doc; | ||
135 | } | 134 | } | ||
136 | 135 | | |||
137 | KSharedConfig::openConfig()->sync(); | 136 | KSharedConfig::openConfig()->sync(); | ||
138 | } | 137 | } | ||
139 | 138 | | |||
140 | QSize KWrite::sizeHint () const | 139 | QSize KWrite::sizeHint () const | ||
141 | { | 140 | { | ||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Line(s) | |||||
238 | void KWrite::modifiedChanged() | 237 | void KWrite::modifiedChanged() | ||
239 | { | 238 | { | ||
240 | documentNameChanged(); | 239 | documentNameChanged(); | ||
241 | m_closeAction->setEnabled(true); | 240 | m_closeAction->setEnabled(true); | ||
242 | } | 241 | } | ||
243 | 242 | | |||
244 | void KWrite::slotNew() | 243 | void KWrite::slotNew() | ||
245 | { | 244 | { | ||
246 | new KWrite(); | 245 | m_app->newWindow(); | ||
247 | } | 246 | } | ||
248 | 247 | | |||
249 | void KWrite::slotOpen() | 248 | void KWrite::slotOpen() | ||
250 | { | 249 | { | ||
251 | const QList<QUrl> urls = QFileDialog::getOpenFileUrls(this, i18n("Open File"), m_view->document()->url()); | 250 | const QList<QUrl> urls = QFileDialog::getOpenFileUrls(this, i18n("Open File"), m_view->document()->url()); | ||
252 | Q_FOREACH(QUrl url, urls) { | 251 | Q_FOREACH(QUrl url, urls) { | ||
253 | slotOpen(url); | 252 | slotOpen(url); | ||
254 | } | 253 | } | ||
255 | } | 254 | } | ||
256 | 255 | | |||
257 | void KWrite::slotOpen(const QUrl &url) | 256 | void KWrite::slotOpen(const QUrl &url) | ||
258 | { | 257 | { | ||
259 | if (url.isEmpty()) { | 258 | if (url.isEmpty()) { | ||
260 | return; | 259 | return; | ||
261 | } | 260 | } | ||
262 | 261 | | |||
263 | if (m_view->document()->isModified() || !m_view->document()->url().isEmpty()) { | 262 | if (m_view->document()->isModified() || !m_view->document()->url().isEmpty()) { | ||
264 | KWrite *t = new KWrite(); | 263 | KWrite *t = m_app->newWindow(); | ||
265 | t->loadURL(url); | 264 | t->loadURL(url); | ||
266 | } else { | 265 | } else { | ||
267 | loadURL(url); | 266 | loadURL(url); | ||
268 | } | 267 | } | ||
269 | } | 268 | } | ||
270 | 269 | | |||
271 | void KWrite::urlChanged() | 270 | void KWrite::urlChanged() | ||
272 | { | 271 | { | ||
273 | if (! m_view->document()->url().isEmpty()) { | 272 | if (! m_view->document()->url().isEmpty()) { | ||
274 | m_recentFiles->addUrl(m_view->document()->url()); | 273 | m_recentFiles->addUrl(m_view->document()->url()); | ||
275 | } | 274 | } | ||
276 | 275 | | |||
277 | // update caption | 276 | // update caption | ||
278 | documentNameChanged(); | 277 | documentNameChanged(); | ||
279 | } | 278 | } | ||
280 | 279 | | |||
281 | void KWrite::newView() | 280 | void KWrite::newView() | ||
282 | { | 281 | { | ||
283 | new KWrite(m_view->document()); | 282 | m_app->newWindow(m_view->document()); | ||
284 | } | 283 | } | ||
285 | 284 | | |||
286 | void KWrite::toggleMenuBar(bool showMessage) | 285 | void KWrite::toggleMenuBar(bool showMessage) | ||
287 | { | 286 | { | ||
288 | if (m_paShowMenuBar->isChecked()) { | 287 | if (m_paShowMenuBar->isChecked()) { | ||
289 | menuBar()->show(); | 288 | menuBar()->show(); | ||
290 | removeMenuBarActionFromContextMenu(); | 289 | removeMenuBarActionFromContextMenu(); | ||
291 | } else { | 290 | } else { | ||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Line(s) | 428 | { | |||
431 | 430 | | |||
432 | m_view->readSessionConfig(KConfigGroup(&config, QStringLiteral("General Options"))); | 431 | m_view->readSessionConfig(KConfigGroup(&config, QStringLiteral("General Options"))); | ||
433 | } | 432 | } | ||
434 | 433 | | |||
435 | void KWrite::saveProperties(KConfigGroup &config) | 434 | void KWrite::saveProperties(KConfigGroup &config) | ||
436 | { | 435 | { | ||
437 | writeConfig(); | 436 | writeConfig(); | ||
438 | 437 | | |||
439 | config.writeEntry("DocumentNumber", docList.indexOf(m_view->document()) + 1); | 438 | config.writeEntry("DocumentNumber", m_app->documents().indexOf(m_view->document()) + 1); | ||
440 | 439 | | |||
441 | KConfigGroup cg(&config, QStringLiteral("General Options")); | 440 | KConfigGroup cg(&config, QStringLiteral("General Options")); | ||
442 | m_view->writeSessionConfig(cg); | 441 | m_view->writeSessionConfig(cg); | ||
443 | } | 442 | } | ||
444 | 443 | | |||
445 | void KWrite::saveGlobalProperties(KConfig *config) //save documents | 444 | void KWrite::saveGlobalProperties(KConfig *config) //save documents | ||
446 | { | 445 | { | ||
447 | config->group("Number").writeEntry("NumberOfDocuments", docList.count()); | 446 | m_app->saveProperties(config); | ||
448 | | ||||
449 | for (int z = 1; z <= docList.count(); z++) { | | |||
450 | QString buf = QStringLiteral("Document %1").arg(z); | | |||
451 | KConfigGroup cg(config, buf); | | |||
452 | KTextEditor::Document *doc = docList.at(z - 1); | | |||
453 | doc->writeSessionConfig(cg); | | |||
454 | } | | |||
455 | | ||||
456 | for (int z = 1; z <= winList.count(); z++) { | | |||
457 | QString buf = QStringLiteral("Window %1").arg(z); | | |||
458 | KConfigGroup cg(config, buf); | | |||
459 | cg.writeEntry("DocumentNumber", docList.indexOf(winList.at(z - 1)->view()->document()) + 1); | | |||
460 | } | | |||
461 | } | | |||
462 | | ||||
463 | //restore session | | |||
464 | void KWrite::restore() | | |||
465 | { | | |||
466 | KConfig *config = KConfigGui::sessionConfig(); | | |||
467 | | ||||
468 | if (!config) { | | |||
469 | return; | | |||
470 | } | | |||
471 | | ||||
472 | int docs, windows; | | |||
473 | QString buf; | | |||
474 | KTextEditor::Document *doc; | | |||
475 | KWrite *t; | | |||
476 | | ||||
477 | KConfigGroup numberConfig(config, "Number"); | | |||
478 | docs = numberConfig.readEntry("NumberOfDocuments", 0); | | |||
479 | windows = numberConfig.readEntry("NumberOfWindows", 0); | | |||
480 | | ||||
481 | for (int z = 1; z <= docs; z++) { | | |||
482 | buf = QStringLiteral("Document %1").arg(z); | | |||
483 | KConfigGroup cg(config, buf); | | |||
484 | doc = KTextEditor::Editor::instance()->createDocument(nullptr); | | |||
485 | doc->readSessionConfig(cg); | | |||
486 | docList.append(doc); | | |||
487 | } | | |||
488 | | ||||
489 | for (int z = 1; z <= windows; z++) { | | |||
490 | buf = QStringLiteral("Window %1").arg(z); | | |||
491 | KConfigGroup cg(config, buf); | | |||
492 | t = new KWrite(docList.at(cg.readEntry("DocumentNumber", 0) - 1)); | | |||
493 | t->restore(config, z); | | |||
494 | } | | |||
495 | } | 447 | } | ||
496 | 448 | | |||
497 | void KWrite::aboutEditor() | 449 | void KWrite::aboutEditor() | ||
498 | { | 450 | { | ||
499 | KAboutApplicationDialog dlg(KTextEditor::Editor::instance()->aboutData(), this); | 451 | KAboutApplicationDialog dlg(KTextEditor::Editor::instance()->aboutData(), this); | ||
500 | dlg.exec(); | 452 | dlg.exec(); | ||
501 | } | 453 | } | ||
502 | 454 | | |||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 498 | if (event->type() == QEvent::FileOpen) { | |||
553 | return true; | 505 | return true; | ||
554 | } | 506 | } | ||
555 | 507 | | |||
556 | /** | 508 | /** | ||
557 | * else: pass over to default implementation | 509 | * else: pass over to default implementation | ||
558 | */ | 510 | */ | ||
559 | return KParts::MainWindow::eventFilter(obj, event); | 511 | return KParts::MainWindow::eventFilter(obj, event); | ||
560 | } | 512 | } | ||
513 | | ||||
514 | QList<KTextEditor::View *> KWrite::views() | ||||
515 | { | ||||
516 | QList<KTextEditor::View *> list; | ||||
517 | list.append(m_view); | ||||
518 | return list; | ||||
519 | } | ||||
520 | | ||||
521 | | ||||
522 | KTextEditor::View *KWrite::activateView(KTextEditor::Document *document) | ||||
523 | { | ||||
524 | if (m_view->document() == document) { | ||||
525 | return m_view; | ||||
526 | } | ||||
527 | | ||||
528 | return nullptr; | ||||
529 | } |