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
|
/*
SPDX-FileCopyrightText: 2005 Roberto Raggi <roberto@kdevelop.org>
SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
SPDX-FileCopyrightText: 2008 Aleix Pol <aleixpol@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "projectmanagerview.h"
#include <QAction>
#include <QHeaderView>
#include <QKeyEvent>
#include <QUrl>
#include <KActionCollection>
#include <KActionMenu>
#include <KLocalizedString>
#include <interfaces/iselectioncontroller.h>
#include <interfaces/context.h>
#include <interfaces/icore.h>
#include <interfaces/isession.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iuicontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/iproject.h>
#include <project/projectproxymodel.h>
#include <project/projectmodel.h>
#include <serialization/indexedstring.h>
#include <util/path.h>
#include "../openwith/iopenwith.h"
#include <sublime/mainwindow.h>
#include <sublime/area.h>
#include "projectmanagerviewplugin.h"
#include "vcsoverlayproxymodel.h"
#include "ui_projectmanagerview.h"
#include "debug.h"
using namespace KDevelop;
ProjectManagerViewItemContext::ProjectManagerViewItemContext(const QList< ProjectBaseItem* >& items, ProjectManagerView* view)
: ProjectItemContextImpl(items), m_view(view)
{
}
ProjectManagerView *ProjectManagerViewItemContext::view() const
{
return m_view;
}
static inline QString sessionConfigGroup()
{
return QStringLiteral("ProjectManagerView");
}
static const char splitterStateConfigKey[] = "splitterState";
static const char syncCurrentDocumentKey[] = "syncCurrentDocument";
static const char targetsVisibleConfigKey[] = "targetsVisible";
static const int projectTreeViewStrechFactor = 75; // %
static const int projectBuildSetStrechFactor = 25; // %
ProjectManagerView::ProjectManagerView( ProjectManagerViewPlugin* plugin, QWidget *parent )
: QWidget( parent ), m_ui(new Ui::ProjectManagerView), m_plugin(plugin)
{
m_ui->setupUi( this );
setFocusProxy(m_ui->projectTreeView);
m_ui->projectTreeView->installEventFilter(this);
setWindowIcon( QIcon::fromTheme( QStringLiteral("project-development"), windowIcon() ) );
setWindowTitle(i18nc("@title:window", "Projects"));
KConfigGroup pmviewConfig(ICore::self()->activeSession()->config(), sessionConfigGroup());
if (pmviewConfig.hasKey(splitterStateConfigKey)) {
QByteArray geometry = pmviewConfig.readEntry<QByteArray>(splitterStateConfigKey, QByteArray());
m_ui->splitter->restoreState(geometry);
} else {
m_ui->splitter->setStretchFactor(0, projectTreeViewStrechFactor);
m_ui->splitter->setStretchFactor(1, projectBuildSetStrechFactor);
}
// keep the project tree view from collapsing (would confuse users)
m_ui->splitter->setCollapsible(0, false);
auto* const syncActionMenu = new KActionMenu(this);
auto* const syncSubAction = plugin->actionCollection()->action(QStringLiteral("locate_document"));
Q_ASSERT(syncSubAction);
for (QAction* action : {static_cast<QAction*>(syncActionMenu), syncSubAction}) {
action->setText(i18nc("@action", "Locate Current Document"));
action->setToolTip(i18nc("@info:tooltip", "Locates the current document in the project tree and selects it."));
action->setIcon(QIcon::fromTheme(QStringLiteral("dirsync")));
connect(action, &QAction::triggered, this, &ProjectManagerView::raiseAndLocateCurrentDocument);
}
syncActionMenu->addAction(syncSubAction);
auto* const autoSyncSubAction = new QAction(i18nc("@action", "Auto-Select Current Document"), this);
autoSyncSubAction->setToolTip(i18nc("@info:tooltip", "Automatically select the current document in the project tree."));
autoSyncSubAction->setCheckable(true);
autoSyncSubAction->setChecked(pmviewConfig.readEntry<bool>(syncCurrentDocumentKey, true));
connect(autoSyncSubAction, &QAction::triggered, this, &ProjectManagerView::toggleSyncCurrentDocument);
connect(ICore::self()->documentController(), &KDevelop::IDocumentController::documentActivated, this, [autoSyncSubAction, this] {
if (autoSyncSubAction->isChecked()) {
locateCurrentDocument();
}
});
// TODO: the above lambda should be connected to IDocumentController::documentUrlChanged as well. However, this
// works incorrectly, because a renamed file is included into its project with a delay. This issue also affects
// other slots connected to documentUrlChanged (see a similar TODO in CompileAnalyzer::CompileAnalyzer()).
syncActionMenu->addAction(autoSyncSubAction);
const auto updateSyncAction = [syncActionMenu, syncSubAction, autoSyncSubAction] {
const bool enable = KDevelop::ICore::self()->documentController()->activeDocument();
syncActionMenu->setEnabled(enable);
syncSubAction->setEnabled(enable);
autoSyncSubAction->setEnabled(enable);
};
addAction(syncActionMenu);
m_toggleTargetsAction = new QAction(i18nc("@action", "Show Build Targets"), this);
m_toggleTargetsAction->setCheckable(true);
m_toggleTargetsAction->setChecked(pmviewConfig.readEntry<bool>(targetsVisibleConfigKey, true));
m_toggleTargetsAction->setIcon(QIcon::fromTheme(QStringLiteral("system-run")));
connect(m_toggleTargetsAction, &QAction::triggered, this, &ProjectManagerView::toggleHideTargets);
addAction(m_toggleTargetsAction);
addAction(plugin->actionCollection()->action(QStringLiteral("project_build")));
addAction(plugin->actionCollection()->action(QStringLiteral("project_install")));
addAction(plugin->actionCollection()->action(QStringLiteral("project_clean")));
connect(m_ui->projectTreeView, &ProjectTreeView::activate, this, &ProjectManagerView::open);
m_ui->buildSetView->setProjectView( this );
m_modelFilter = new ProjectProxyModel( this );
m_modelFilter->showTargets(m_toggleTargetsAction->isChecked());
m_modelFilter->setSourceModel(ICore::self()->projectController()->projectModel());
m_overlayProxy = new VcsOverlayProxyModel( this );
m_overlayProxy->setSourceModel(m_modelFilter);
m_ui->projectTreeView->setModel( m_overlayProxy );
connect( m_ui->projectTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &ProjectManagerView::selectionChanged );
connect( KDevelop::ICore::self()->documentController(), &IDocumentController::documentClosed,
this, updateSyncAction);
connect( KDevelop::ICore::self()->documentController(), &IDocumentController::documentActivated,
this, updateSyncAction);
connect( qobject_cast<Sublime::MainWindow*>(KDevelop::ICore::self()->uiController()->activeMainWindow()), &Sublime::MainWindow::areaChanged,
this, updateSyncAction);
selectionChanged();
updateSyncAction();
//Update the "sync" button after the initialization has completed, to see whether there already is some open documents
QMetaObject::invokeMethod(this, updateSyncAction, Qt::QueuedConnection);
// Need to set this to get horizontal scrollbar. Also needs to be done after
// the setModel call
m_ui->projectTreeView->header()->setSectionResizeMode( QHeaderView::ResizeToContents );
}
bool ProjectManagerView::eventFilter(QObject* obj, QEvent* event)
{
if (obj == m_ui->projectTreeView) {
if (event->type() == QEvent::KeyRelease) {
auto* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Delete && keyEvent->modifiers() == Qt::NoModifier) {
m_plugin->removeItems(selectedItems());
return true;
} else if (keyEvent->key() == Qt::Key_F2 && keyEvent->modifiers() == Qt::NoModifier) {
m_plugin->renameItems(selectedItems());
return true;
} else if (keyEvent->key() == Qt::Key_C && keyEvent->modifiers() == Qt::ControlModifier) {
m_plugin->copyFromContextMenu();
return true;
} else if (keyEvent->key() == Qt::Key_V && keyEvent->modifiers() == Qt::ControlModifier) {
m_plugin->pasteFromContextMenu();
return true;
}
}
}
return QObject::eventFilter(obj, event);
}
void ProjectManagerView::selectionChanged()
{
m_ui->buildSetView->selectionChanged();
QList<ProjectBaseItem*> selected;
const auto selectedRows = m_ui->projectTreeView->selectionModel()->selectedRows();
selected.reserve(selectedRows.size());
for (const auto& idx : selectedRows) {
selected << ICore::self()->projectController()->projectModel()->itemFromIndex(indexFromView( idx ));
}
selected.removeAll(nullptr);
KDevelop::ICore::self()->selectionController()->updateSelection( new ProjectManagerViewItemContext( selected, this ) );
}
ProjectManagerView::~ProjectManagerView()
{
KConfigGroup pmviewConfig(ICore::self()->activeSession()->config(), sessionConfigGroup());
pmviewConfig.writeEntry(splitterStateConfigKey, m_ui->splitter->saveState());
pmviewConfig.sync();
delete m_ui;
}
QList<KDevelop::ProjectBaseItem*> ProjectManagerView::selectedItems() const
{
QList<KDevelop::ProjectBaseItem*> items;
const auto selectedIndexes = m_ui->projectTreeView->selectionModel()->selectedIndexes();
for (const QModelIndex& idx : selectedIndexes) {
KDevelop::ProjectBaseItem* item = ICore::self()->projectController()->projectModel()->itemFromIndex(indexFromView(idx));
if( item )
items << item;
else
qCDebug(PLUGIN_PROJECTMANAGERVIEW) << "adding an unknown item";
}
return items;
}
void ProjectManagerView::selectItems(const QList< ProjectBaseItem* >& items)
{
QItemSelection selection;
selection.reserve(items.size());
for (ProjectBaseItem *item : items) {
QModelIndex indx = indexToView(item->index());
selection.append(QItemSelectionRange(indx, indx));
m_ui->projectTreeView->setCurrentIndex(indx);
}
m_ui->projectTreeView->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
}
void ProjectManagerView::expandItem(ProjectBaseItem* item)
{
m_ui->projectTreeView->expand( indexToView(item->index()));
}
void ProjectManagerView::toggleHideTargets(bool visible)
{
KConfigGroup pmviewConfig(ICore::self()->activeSession()->config(), sessionConfigGroup());
pmviewConfig.writeEntry<bool>(targetsVisibleConfigKey, visible);
m_modelFilter->showTargets(visible);
}
void ProjectManagerView::toggleSyncCurrentDocument(bool sync)
{
KConfigGroup pmviewConfig(ICore::self()->activeSession()->config(), sessionConfigGroup());
pmviewConfig.writeEntry<bool>(syncCurrentDocumentKey, sync);
if (sync) {
raiseAndLocateCurrentDocument();
}
}
void ProjectManagerView::raiseAndLocateCurrentDocument()
{
ICore::self()->uiController()->raiseToolView(this);
locateCurrentDocument();
}
void ProjectManagerView::locateCurrentDocument()
{
KDevelop::IDocument *doc = ICore::self()->documentController()->activeDocument();
if (!doc) {
// in theory we should never get a null pointer as the action is only enabled
// when there is an active document.
// but: in practice it can happen that you close the last document and press
// the shortcut to locate a doc or vice versa... so just do the failsafe thing here...
return;
}
QModelIndex bestMatch;
const auto projects = ICore::self()->projectController()->projects();
for (IProject* proj : projects) {
const auto files = proj->filesForPath(IndexedString(doc->url()));
for (KDevelop::ProjectFileItem* item : files) {
QModelIndex index = indexToView(item->index());
if (index.isValid()) {
if (!bestMatch.isValid()) {
bestMatch = index;
} else if (KDevelop::ProjectBaseItem* parent = item->parent()) {
// prefer files in their real folders over the 'copies' in the target folders
if (!parent->target()) {
bestMatch = index;
break;
}
}
}
}
}
if (bestMatch.isValid()) {
m_ui->projectTreeView->clearSelection();
m_ui->projectTreeView->setCurrentIndex(bestMatch);
m_ui->projectTreeView->expand(bestMatch);
m_ui->projectTreeView->scrollTo(bestMatch);
}
}
void ProjectManagerView::open( const Path& path )
{
IOpenWith::openFiles(QList<QUrl>() << path.toUrl());
}
QModelIndex ProjectManagerView::indexFromView(const QModelIndex& index) const
{
return m_modelFilter->mapToSource( m_overlayProxy->mapToSource(index) );
}
QModelIndex ProjectManagerView::indexToView(const QModelIndex& index) const
{
return m_overlayProxy->mapFromSource( m_modelFilter->mapFromSource(index) );
}
#include "moc_projectmanagerview.cpp"
|