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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
/*
SPDX-FileCopyrightText: 2002 Falk Brettschneider <falkbr@kdevelop.org>
SPDX-FileCopyrightText: 2003 John Firebaugh <jfirebaugh@kde.org>
SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
SPDX-FileCopyrightText: 2006, 2007 Alexander Dymo <adymo@kdevelop.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "mainwindow.h"
#include "mainwindow_p.h"
#include <QDBusConnection>
#include <QDomElement>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMenu>
#include <QMenuBar>
#include <QMimeData>
#include <QUrl>
#include <KActionCollection>
#include <KLocalizedString>
#include <KShortcutsDialog>
#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KWindowSystem>
#include <KXMLGUIFactory>
#include <sublime/area.h>
#include "shellextension.h"
#include "partcontroller.h"
#include "plugincontroller.h"
#include "projectcontroller.h"
#include "uicontroller.h"
#include "documentcontroller.h"
#include "workingsetcontroller.h"
#include "sessioncontroller.h"
#include "sourceformattercontroller.h"
#include "areadisplay.h"
#include "project.h"
#include "debug.h"
#include "uiconfig.h"
#include "ktexteditorpluginintegration.h"
#include <interfaces/isession.h>
#include <interfaces/iprojectcontroller.h>
#include <sublime/view.h>
#include <sublime/document.h>
#include <sublime/urldocument.h>
#include <sublime/container.h>
#include <util/path.h>
#include <util/widgetcolorizer.h>
using namespace KDevelop;
namespace {
QColor defaultColor(const QPalette& palette)
{
return palette.windowText().color();
}
QColor colorForDocument(const QUrl& url, const QPalette& palette, const QColor& defaultColor)
{
auto project = Core::self()->projectController()->findProjectForUrl(url);
if (!project)
return defaultColor;
return WidgetColorizer::colorForId(qHash(project->path()), palette);
}
}
void MainWindow::applyMainWindowSettings(const KConfigGroup& config)
{
Q_D(MainWindow);
if(!d->changingActiveView())
KXmlGuiWindow::applyMainWindowSettings(config);
}
void MainWindow::createGUI(KParts::Part* part)
{
Sublime::MainWindow::setWindowTitleHandling(false);
Sublime::MainWindow::createGUI(part);
}
void MainWindow::initializeCorners()
{
const KConfigGroup cg = KSharedConfig::openConfig()->group( "UiSettings" );
const int bottomleft = cg.readEntry( "BottomLeftCornerOwner", 0 );
const int bottomright = cg.readEntry( "BottomRightCornerOwner", 0 );
qCDebug(SHELL) << "Bottom Left:" << bottomleft;
qCDebug(SHELL) << "Bottom Right:" << bottomright;
// 0 means vertical dock (left, right), 1 means horizontal dock( top, bottom )
if( bottomleft == 0 )
setCorner( Qt::BottomLeftCorner, Qt::LeftDockWidgetArea );
else if( bottomleft == 1 )
setCorner( Qt::BottomLeftCorner, Qt::BottomDockWidgetArea );
if( bottomright == 0 )
setCorner( Qt::BottomRightCorner, Qt::RightDockWidgetArea );
else if( bottomright == 1 )
setCorner( Qt::BottomRightCorner, Qt::BottomDockWidgetArea );
}
MainWindow::MainWindow( Sublime::Controller *parent, Qt::WindowFlags flags )
: Sublime::MainWindow( parent, flags )
{
QDBusConnection::sessionBus().registerObject( QStringLiteral("/kdevelop/MainWindow"),
this, QDBusConnection::ExportScriptableSlots );
setAcceptDrops( true );
initializeCorners();
setObjectName( QStringLiteral("MainWindow") );
d_ptr = new MainWindowPrivate(this);
Q_D(MainWindow);
setStandardToolBarMenuEnabled( true );
d->setupActions();
if( !ShellExtension::getInstance()->xmlFile().isEmpty() )
{
setXMLFile( ShellExtension::getInstance() ->xmlFile() );
}
menuBar()->setCornerWidget(new AreaDisplay(this), Qt::TopRightCorner);
}
MainWindow::~ MainWindow()
{
if (memberList().count() == 1) {
// We're closing down...
Core::self()->shutdown();
}
delete d_ptr;
}
KTextEditorIntegration::MainWindow *MainWindow::kateWrapper() const
{
Q_D(const MainWindow);
return d->kateWrapper();
}
void MainWindow::split(Qt::Orientation orientation)
{
Q_D(MainWindow);
d->split(orientation);
}
void MainWindow::ensureVisible()
{
if (isMinimized()) {
if (isMaximized()) {
showMaximized();
} else {
showNormal();
}
}
KWindowSystem::forceActiveWindow(winId());
}
QAction* MainWindow::createCustomElement(QWidget* parent, int index, const QDomElement& element)
{
QAction* before = nullptr;
if (index > 0 && index < parent->actions().count())
before = parent->actions().at(index);
//KDevelop needs to ensure that separators defined as <Separator style="visible" />
//are always shown in the menubar. For those, we create special disabled actions
//instead of calling QMenuBar::addSeparator() because menubar separators are ignored
if (element.tagName().compare(QLatin1String("separator"), Qt::CaseInsensitive) == 0
&& element.attribute(QStringLiteral("style")) == QLatin1String("visible")) {
if ( auto* bar = qobject_cast<QMenuBar*>( parent ) ) {
auto* separatorAction = new QAction(QStringLiteral("|"), this);
bar->insertAction( before, separatorAction );
separatorAction->setDisabled(true);
return separatorAction;
}
}
return KXMLGUIBuilder::createCustomElement(parent, index, element);
}
bool KDevelop::MainWindow::event( QEvent* ev )
{
if ( ev->type() == QEvent::PaletteChange )
updateAllTabColors();
return Sublime::MainWindow::event(ev);
}
void MainWindow::dragEnterEvent( QDragEnterEvent* ev )
{
const QMimeData* mimeData = ev->mimeData();
if (mimeData->hasUrls()) {
ev->acceptProposedAction();
} else if (mimeData->hasText()) {
// also take text which contains a URL
const QUrl url = QUrl::fromUserInput(mimeData->text());
if (url.isValid()) {
ev->acceptProposedAction();
}
}
}
void MainWindow::dropEvent( QDropEvent* ev )
{
Sublime::View* dropToView = viewForPosition(mapToGlobal(ev->pos()));
if(dropToView)
activateView(dropToView);
QList<QUrl> urls;
const QMimeData* mimeData = ev->mimeData();
if (mimeData->hasUrls()) {
urls = mimeData->urls();
} else if (mimeData->hasText()) {
const QUrl url = QUrl::fromUserInput(mimeData->text());
if (url.isValid()) {
urls << url;
}
}
bool eventUsed = false;
if (urls.size() == 1) {
eventUsed = Core::self()->projectControllerInternal()->fetchProjectFromUrl(urls.at(0), ProjectController::NoFetchFlags);
}
if (!eventUsed) {
for(const auto& url : qAsConst(urls)) {
Core::self()->documentController()->openDocument(url);
}
}
ev->acceptProposedAction();
}
void MainWindow::loadSettings()
{
qCDebug(SHELL) << "Loading Settings";
initializeCorners();
updateAllTabColors();
Sublime::MainWindow::loadSettings();
}
void MainWindow::configureShortcuts()
{
///Workaround for a problem with the actions: Always start the shortcut-configuration in the first mainwindow, then propagate the updated
///settings into the other windows
// We need to bring up the shortcut dialog ourself instead of
// Core::self()->uiControllerInternal()->mainWindows()[0]->guiFactory()->configureShortcuts();
// so we can connect to the saved() signal to propagate changes in the editor shortcuts
KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
const auto firstMainWindowClientsBefore = Core::self()->uiControllerInternal()->mainWindows()[0]->guiFactory()->clients();
for (KXMLGUIClient* client : firstMainWindowClientsBefore) {
if(client && !client->xmlFile().isEmpty())
dlg.addCollection( client->actionCollection() );
}
connect(&dlg, &KShortcutsDialog::saved, this, &MainWindow::shortcutsChanged);
dlg.configure(true);
QMap<QString, QKeySequence> shortcuts;
// querying again just in case something changed behind our back
const auto firstMainWindowClientsAfter = Core::self()->uiControllerInternal()->mainWindows()[0]->guiFactory()->clients();
for (KXMLGUIClient* client : firstMainWindowClientsAfter) {
const auto actions = client->actionCollection()->actions();
for (QAction* action : actions) {
if(!action->objectName().isEmpty()) {
shortcuts[action->objectName()] = action->shortcut();
}
}
}
for(int a = 1; a < Core::self()->uiControllerInternal()->mainWindows().size(); ++a) {
const auto clients = Core::self()->uiControllerInternal()->mainWindows()[a]->guiFactory()->clients();
for (KXMLGUIClient* client : clients) {
const auto actions = client->actionCollection()->actions();
for (QAction* action : actions) {
qCDebug(SHELL) << "transferring setting shortcut for" << action->objectName();
const auto shortcutIt = shortcuts.constFind(action->objectName());
if (shortcutIt != shortcuts.constEnd()) {
action->setShortcut(*shortcutIt);
}
}
}
}
}
void MainWindow::shortcutsChanged()
{
KTextEditor::View *activeClient = Core::self()->documentController()->activeTextDocumentView();
if(!activeClient)
return;
const auto documents = Core::self()->documentController()->openDocuments();
for (IDocument* doc : documents) {
KTextEditor::Document *textDocument = doc->textDocument();
if (textDocument) {
const auto views = textDocument->views();
for (KTextEditor::View* client : views) {
if (client != activeClient) {
client->reloadXML();
}
}
}
}
}
void MainWindow::initialize()
{
Q_D(MainWindow);
KStandardAction::keyBindings(this, SLOT(configureShortcuts()), actionCollection());
setupGUI(ToolBar | Save);
createGUI(nullptr);
Core::self()->partController()->addManagedTopLevelWidget(this);
qCDebug(SHELL) << "Adding plugin-added connection";
connect( Core::self()->pluginController(), &IPluginController::pluginLoaded,
d, &MainWindowPrivate::addPlugin);
connect( Core::self()->pluginController(), &IPluginController::pluginUnloaded,
d, &MainWindowPrivate::removePlugin);
connect( Core::self()->partController(), &IPartController::activePartChanged,
d, &MainWindowPrivate::activePartChanged);
connect( this, &MainWindow::activeViewChanged,
d, &MainWindowPrivate::changeActiveView);
connect(Core::self()->sourceFormatterControllerInternal(), &SourceFormatterController::hasFormattersChanged,
d, &MainWindowPrivate::updateSourceFormatterGuiClient);
const auto plugins = Core::self()->pluginController()->loadedPlugins();
for (IPlugin* plugin : plugins) {
d->addPlugin(plugin);
}
guiFactory()->addClient(Core::self()->sessionController());
if (Core::self()->sourceFormatterControllerInternal()->hasFormatters()) {
guiFactory()->addClient(Core::self()->sourceFormatterControllerInternal());
}
// Needed to re-plug the actions from the sessioncontroller as xmlguiclients don't
// seem to remember which actions where plugged in.
Core::self()->sessionController()->updateXmlGuiActionList();
d->setupGui();
qRegisterMetaType<QPointer<KTextEditor::Document>>();
//Queued so we process it with some delay, to make sure the rest of the UI has already adapted
connect(Core::self()->documentController(), &IDocumentController::documentActivated,
// Use a queued connection, because otherwise the view is not yet fully set up
// but wrap the document in a smart pointer to guard against crashes when it
// gets deleted in the meantime
this, [this](IDocument *doc) {
const auto textDocument = QPointer<KTextEditor::Document>(doc->textDocument());
QMetaObject::invokeMethod(this, "documentActivated", Qt::QueuedConnection,
Q_ARG(QPointer<KTextEditor::Document>, textDocument));
});
connect(Core::self()->documentController(), &IDocumentController::documentClosed, this, &MainWindow::updateCaption, Qt::QueuedConnection);
connect(Core::self()->documentController(), &IDocumentController::documentUrlChanged, this, &MainWindow::updateCaption, Qt::QueuedConnection);
connect(Core::self()->documentController(), &IDocumentController::documentStateChanged, this, &MainWindow::updateCaption, Qt::QueuedConnection);
connect(Core::self()->sessionController()->activeSession(), &ISession::sessionUpdated, this, &MainWindow::updateCaption);
// if currently viewed document is part of project, trigger update of full path to project prefixed one
connect(Core::self()->projectController(), &ProjectController::projectOpened, this, &MainWindow::updateCaption, Qt::QueuedConnection);
connect(Core::self()->documentController(), &IDocumentController::documentOpened, this, &MainWindow::updateTabColor);
connect(Core::self()->documentController(), &IDocumentController::documentUrlChanged, this, &MainWindow::updateTabColor);
connect(this, &Sublime::MainWindow::viewAdded, this, &MainWindow::updateAllTabColors);
connect(Core::self()->projectController(), &ProjectController::projectOpened, this, &MainWindow::updateAllTabColors, Qt::QueuedConnection);
updateCaption();
}
void MainWindow::cleanup()
{
}
void MainWindow::setVisible( bool visible )
{
KXmlGuiWindow::setVisible( visible );
emit finishedLoading();
}
bool MainWindow::queryClose()
{
if (!Core::self()->documentControllerInternal()->saveAllDocumentsForWindow(this, IDocument::Default))
return false;
return Sublime::MainWindow::queryClose();
}
void MainWindow::documentActivated(const QPointer<KTextEditor::Document>& textDocument)
{
Q_D(MainWindow);
updateCaption();
// update active document connection
disconnect(d->activeDocumentReadWriteConnection);
if (textDocument) {
d->activeDocumentReadWriteConnection = connect(textDocument, &KTextEditor::Document::readWriteChanged,
this, &MainWindow::updateCaption);
}
}
void MainWindow::updateCaption()
{
QString title;
QString localFilePath;
bool isDocumentModified = false;
if(area()->activeView())
{
Sublime::Document* doc = area()->activeView()->document();
auto* urlDoc = qobject_cast<Sublime::UrlDocument*>(doc);
if(urlDoc)
{
if (urlDoc->url().isLocalFile()) {
localFilePath = urlDoc->url().toLocalFile();
}
title += Core::self()->projectController()->prettyFileName(urlDoc->url(), KDevelop::IProjectController::FormatPlain);
}
else
title += doc->title();
auto iDoc = qobject_cast<IDocument*>(doc);
if (iDoc && iDoc->textDocument() && !iDoc->textDocument()->isReadWrite()) {
title += i18n(" (read only)");
}
title += QLatin1String(" [*]"); // [*] is placeholder for modifed state, cmp. QWidget::windowModified
isDocumentModified = iDoc && (iDoc->state() != IDocument::Clean);
}
const auto activeSession = Core::self()->sessionController()->activeSession();
const QString sessionTitle = activeSession ? activeSession->description() : QString();
if (!sessionTitle.isEmpty()) {
if (title.isEmpty()) {
title = sessionTitle;
} else {
title = sessionTitle + QLatin1String(" - [ ") + title + QLatin1Char(']');
}
}
// Workaround for a bug observed on macOS with Qt 5.9.8 (TODO: test with newer Qt, report bug):
// Ensure to call setCaption() (thus implicitly setWindowTitle()) before
// setWindowModified() & setWindowFilePath().
// Otherwise, if the state will change "modifed" from true to false as well change the title string,
// calling setWindowTitle() last results in the "modified" indicator==asterisk becoming part of the
// displayed window title somehow.
// Other platforms so far not known to be affected, any order of calls seems fine.
setCaption(title);
setWindowModified(isDocumentModified);
setWindowFilePath(localFilePath);
}
void MainWindow::updateAllTabColors()
{
auto documentController = Core::self()->documentController();
if (!documentController)
return;
const auto defaultColor = ::defaultColor(palette());
if (UiConfig::colorizeByProject()) {
QHash<const Sublime::View*, QColor> viewColors;
const auto containers = this->containers();
for (auto* container : containers) {
const auto views = container->views();
viewColors.reserve(views.size());
viewColors.clear();
for (auto view : views) {
const auto urlDoc = qobject_cast<Sublime::UrlDocument*>(view->document());
if (urlDoc) {
viewColors[view] = colorForDocument(urlDoc->url(), palette(), defaultColor);
}
}
container->setTabColors(viewColors);
}
} else {
const auto containers = this->containers();
for (auto* container : containers) {
container->resetTabColors(defaultColor);
}
}
}
void MainWindow::updateTabColor(IDocument* doc)
{
if (!UiConfig::self()->colorizeByProject())
return;
const auto color = colorForDocument(doc->url(), palette(), defaultColor(palette()));
const auto containers = this->containers();
for (auto* container : containers) {
const auto views = container->views();
for (auto* view : views) {
const auto urlDoc = qobject_cast<Sublime::UrlDocument*>(view->document());
if (urlDoc && urlDoc->url() == doc->url()) {
container->setTabColor(view, color);
}
}
}
}
void MainWindow::registerStatus(QObject* status)
{
Q_D(MainWindow);
d->registerStatus(status);
}
void MainWindow::initializeStatusBar()
{
Q_D(MainWindow);
d->setupStatusBar();
}
void MainWindow::showErrorMessage(const QString& message, int timeout)
{
Q_D(MainWindow);
d->showErrorMessage(message, timeout);
}
void MainWindow::tabContextMenuRequested(Sublime::View* view, QMenu* menu)
{
Q_D(MainWindow);
Sublime::MainWindow::tabContextMenuRequested(view, menu);
d->tabContextMenuRequested(view, menu);
}
void MainWindow::tabToolTipRequested(Sublime::View* view, Sublime::Container* container, int tab)
{
Q_D(MainWindow);
d->tabToolTipRequested(view, container, tab);
}
void MainWindow::dockBarContextMenuRequested(Qt::DockWidgetArea area, const QPoint& position)
{
Q_D(MainWindow);
d->dockBarContextMenuRequested(area, position);
}
void MainWindow::newTabRequested()
{
Q_D(MainWindow);
Sublime::MainWindow::newTabRequested();
d->fileNew();
}
|