-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
214 lines (169 loc) · 6.46 KB
/
mainwindow.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Created by john on 1/28/17.
//
#include <QApplication>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QBitmap>
#include <QMenuBar>
#include <QSyntaxHighlighter>
#include <QLayout>
#include <QVariantList>
#include <KLocalizedString>
#include <KActionCollection>
#include <KStandardAction>
#include <QTextEdit>
#include <KMessageBox>
#include <kshortcutsdialog.h>
#include <KXMLGUIFactory>
#include <KEditToolBar>
#include <KAboutApplicationDialog>
#include <ktexteditor/editor.h>
#include <kparts/readwritepart.h>
#include <KService/KMimeTypeTrader>
#include "mainwindow.h"
#include "opendialog.h"
KDiffMainWindow::KDiffMainWindow(QWidget *parent)
: MainWindow(parent)
, settings(Settings::instance())
{
m_viewPart = KMimeTypeTrader::createInstanceFromQuery<KParts::ReadWritePart>("text/x-patch", "KDiff/ViewPart", this);
if ( m_viewPart )
{
setCentralWidget( m_viewPart->widget() );
setupActions();
setXMLFile("kdiffui.rc");
createShellGUI(true);
setAutoSaveSettings();
createGUI(m_viewPart);
}
else
{
// if we couldn't load our Part, we exit since the Shell by
// itself can't do anything useful
KMessageBox::error(this, i18n( "Could not load our KDiffViewPart." ) );
exit(2);
}
}
KDiffMainWindow::~KDiffMainWindow() {
}
void KDiffMainWindow::setupActions() {
QAction *a = new QAction(this);
a->setText(i18n("C&ompare Files"));
a->setIcon(QIcon::fromTheme("document-open-folder"));
actionCollection()->setDefaultShortcut(a, Qt::CTRL + Qt::Key_O);
actionCollection()->addAction("compare_files", a);
connect(a, SIGNAL(triggered(bool)), this, SLOT(openFiles()));
a = new QAction(this);
a->setText(i18n("Open &Diff"));
a->setIcon(QIcon::fromTheme("document-open"));
actionCollection()->setDefaultShortcut(a, Qt::CTRL + Qt::Key_D);
actionCollection()->addAction("open_diff", a);
connect(a, SIGNAL(triggered(bool)), this, SLOT(openDiff()));
a = new QAction(this);
a->setText(i18n("&Blend Diff and Files"));
// a->setIcon(QIcon::fromTheme("document-open"));
actionCollection()->setDefaultShortcut(a, Qt::CTRL + Qt::Key_B);
actionCollection()->addAction("blend_diff", a);
connect(a, SIGNAL(triggered(bool)), this, SLOT(openDiffAndFiles()));
KStandardAction::quit(this, SLOT(quit()), actionCollection());
m_paShowMenuBar = KStandardAction::showMenubar(this, SLOT(toggleMenuBar()), actionCollection());
setStandardToolBarMenuEnabled(true);
a = actionCollection()->addAction(KStandardAction::ConfigureToolbars, QStringLiteral("options_configure_toolbars"),
this, SLOT(editToolbars()));
a->setWhatsThis(i18n("Configure which items should appear in the toolbar(s)."));
a = actionCollection()->addAction(QStringLiteral("help_about_editor"));
a->setText(i18n("&About Editor Component"));
connect(a, SIGNAL(triggered()), this, SLOT(aboutEditor()));
a = actionCollection()->addAction(KStandardAction::KeyBindings, m_viewPart, SLOT(editKeys()));
a->setWhatsThis(i18n("Configure the application's keyboard shortcut assignments."));
swapAction = new QAction(this);
swapAction->setEnabled(false);
swapAction->setText(i18n("&Swap Source and Destination"));
actionCollection()->addAction("swap", swapAction);
connect(swapAction, SIGNAL(triggered(bool)), this, SLOT(swap()));
}
void KDiffMainWindow::openFiles() {
OpenDialog d(this);
int status = d.chooseFiles();
if (status == QDialog::Accepted) {
settings->saveRecentSourceUrls(d.sourceList());
settings->saveRecentDestinationUrls(d.destinationList());
QUrl source = QUrl(settings->recentSourceUrls().at(0));
QUrl destination = QUrl(settings->recentDestinationUrls().at(0));
viewPart()->openFiles(source, destination);
swapAction->setEnabled(true);
}
}
void KDiffMainWindow::openDiff() {
OpenDialog d(this);
int status = d.chooseDiff();
if (status == QDialog::Accepted) {
settings->saveRecentPatchUrls(d.patchList());
QUrl diffUrl = QUrl(settings->recentPatchUrls().at(0));
viewPart()->openDiff(diffUrl);
swapAction->setEnabled(false);
}
}
void KDiffMainWindow::openDiffAndFiles() {
OpenDialog d(this);
int status = d.chooseDiffAndFiles();
if (status == QDialog::Accepted) {
settings->saveRecentPatchUrls(d.patchList());
settings->saveRecentDestinationUrls(d.destinationList());
QUrl diffUrl = QUrl(settings->recentPatchUrls().at(0));
QUrl destination = QUrl(settings->recentDestinationUrls().at(0));
viewPart()->openDiffAndFiles(diffUrl, destination);
swapAction->setEnabled(false);
}
}
void KDiffMainWindow::editToolbars()
{
KConfigGroup cfg = KSharedConfig::openConfig()->group("MainWindow");
saveMainWindowSettings(cfg);
KEditToolBar dlg(guiFactory(), this);
// connect(&dlg, SIGNAL(newToolBarConfig()), this, SLOT(slotNewToolbarConfig()));
dlg.exec();
}
void KDiffMainWindow::aboutEditor()
{
KAboutApplicationDialog dlg(KTextEditor::Editor::instance()->aboutData(), this);
dlg.exec();
}
void KDiffMainWindow::toggleMenuBar()
{
if (m_paShowMenuBar->isChecked()) {
menuBar()->show();
} else {
const QString accel = m_paShowMenuBar->shortcut().toString();
if (KMessageBox::warningContinueCancel(this, i18n("This will hide the menu bar completely."
" You can show it again by typing %1.", accel),
i18n("Hide menu bar"), KStandardGuiItem::cont(),
KStandardGuiItem::cancel(),
QLatin1String("HideMenuBarWarning")) == KMessageBox::Continue) {
menuBar()->hide();
}
}
}
void KDiffMainWindow::quit() {
if(m_viewPart->queryClose()) {
qApp->quit();
}
}
void KDiffMainWindow::closeEvent(QCloseEvent *event) {
if(m_viewPart->queryClose()) {
KMainWindow::closeEvent(event);
} else {
event->ignore();
}
}
KDiffInterface* KDiffMainWindow::viewPart() const
{
return qobject_cast<KDiffInterface *>(m_viewPart);
}
void KDiffMainWindow::swap() {
QUrl source = QUrl(settings->recentSourceUrls().at(0));
QUrl destination = QUrl(settings->recentDestinationUrls().at(0));
viewPart()->openFiles(destination, source);
}