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
|
/*
SPDX-FileCopyrightText: 2009, 2013 Andreas Pakulat <apaku@gmx.de>
SPDX-FileCopyrightText: 2013 Jarosław Sierant <jaroslaw.sierant@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "documentswitcherplugin.h"
#include <QApplication>
#include <QListView>
#include <QStandardItemModel>
#include <QScrollBar>
#include <KActionCollection>
#include <KLocalizedString>
#include <KPluginFactory>
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>
#include <interfaces/idocument.h>
#include <interfaces/idocumentcontroller.h>
#include <sublime/mainwindow.h>
#include "documentswitchertreeview.h"
#include "documentswitcheritem.h"
#include "debug.h"
#include <algorithm>
K_PLUGIN_FACTORY_WITH_JSON(DocumentSwitcherFactory, "kdevdocumentswitcher.json", registerPlugin<DocumentSwitcherPlugin>();)
//TODO: Show frame around view's widget while walking through
//TODO: Make the widget transparent
DocumentSwitcherPlugin::DocumentSwitcherPlugin(QObject* parent, const KPluginMetaData& metaData,
const QVariantList& /*args*/)
: KDevelop::IPlugin(QStringLiteral("kdevdocumentswitcher"), parent, metaData)
, view(nullptr)
{
setXMLFile(QStringLiteral("kdevdocumentswitcher.rc"));
qCDebug(PLUGIN_DOCUMENTSWITCHER) << "Adding active mainwindow from constructor";
KDevelop::IDocumentController *documentController = KDevelop::ICore::self()->documentController();
for (KDevelop::IDocument *doc : documentController->openDocuments()) {
documentOpened(doc);
}
// Signals to track last used documents.
connect(documentController, &KDevelop::IDocumentController::documentOpened, this, &DocumentSwitcherPlugin::documentOpened);
connect(documentController, &KDevelop::IDocumentController::documentActivated, this, &DocumentSwitcherPlugin::documentActivated);
connect(documentController, &KDevelop::IDocumentController::documentClosed, this, &DocumentSwitcherPlugin::documentClosed);
#ifdef Q_OS_MACOS
// Qt/Mac swaps the Ctrl and Meta (Command) keys by default, so that shortcuts defined as Ctrl+X
// become the platform-standard Command+X . Ideally we would map the document switcher shortcut to
// Control+Tab (and thus Qt::META|Qt::Key_Tab) everywhere because Command+Tab and Command+Shift+Tab
// are reserved system shortcuts that bring up the application switcher. The Control+Tab shortcut is
// inoperable on Mac, so we resort to the Alt (Option) key, unless the AA_MacDontSwapCtrlAndMeta
// attribute is set.
const Qt::Modifier shortcutAccelerator = QCoreApplication::testAttribute(Qt::AA_MacDontSwapCtrlAndMeta) ? Qt::CTRL : Qt::ALT;
#else
const Qt::Modifier shortcutAccelerator = Qt::CTRL;
#endif
forwardAction = actionCollection()->addAction ( QStringLiteral( "last_used_views_forward" ) );
forwardAction->setText(i18nc("@action", "Last Used Views"));
forwardAction->setIcon( QIcon::fromTheme( QStringLiteral( "go-next-view-page") ) );
actionCollection()->setDefaultShortcut( forwardAction, shortcutAccelerator | Qt::Key_Tab );
forwardAction->setWhatsThis(i18nc("@info:whatsthis", "Opens a list to walk through the list of last used views."));
forwardAction->setToolTip( i18nc("@info:tooltip", "Walk through the list of last used views"));
connect( forwardAction, &QAction::triggered, this, &DocumentSwitcherPlugin::walkForward );
backwardAction = actionCollection()->addAction ( QStringLiteral( "last_used_views_backward" ) );
backwardAction->setText(i18nc("@action", "Last Used Views (Reverse)"));
backwardAction->setIcon( QIcon::fromTheme( QStringLiteral( "go-previous-view-page") ) );
actionCollection()->setDefaultShortcut( backwardAction, shortcutAccelerator | Qt::SHIFT | Qt::Key_Tab );
backwardAction->setWhatsThis(i18nc("@info:whatsthis", "Opens a list to walk through the list of last used views in reverse."));
backwardAction->setToolTip(i18nc("@info:tooltip", "Walk through the list of last used views"));
connect( backwardAction, &QAction::triggered, this, &DocumentSwitcherPlugin::walkBackward );
view = new DocumentSwitcherTreeView( this );
view->setSelectionBehavior( QAbstractItemView::SelectRows );
view->setSelectionMode( QAbstractItemView::SingleSelection );
view->setUniformRowHeights( true );
view->setTextElideMode( Qt::ElideMiddle );
view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view->addAction( forwardAction );
view->addAction( backwardAction );
view->setHeaderHidden( true );
view->setIndentation( 10 );
connect( view, &QListView::pressed, this, &DocumentSwitcherPlugin::switchToClicked );
connect( view, &QListView::activated, this, &DocumentSwitcherPlugin::itemActivated );
model = new QStandardItemModel( view );
view->setModel( model );
}
void DocumentSwitcherPlugin::setViewGeometry(Sublime::MainWindow* window)
{
const QSize centralSize = window->centralWidget()->size();
// Maximum size of the view is 3/4th of the central widget (the editor area) so the view does not overlap the
// mainwindow since that looks awkward.
const QSize viewMaxSize( centralSize.width() * 3/4, centralSize.height() * 3/4 );
// The actual view size should be as big as the columns/rows need it, but smaller than the max-size. This means
// the view will get quite high with many open files but I think that is ok. Otherwise one can easily tweak the
// max size to be only 1/2th of the central widget size
const int rowHeight = view->sizeHintForRow(0);
const int frameWidth = view->frameWidth();
const QSize viewSize( std::min( view->sizeHintForColumn(0) + 2 * frameWidth + view->verticalScrollBar()->width(), viewMaxSize.width() ),
std::min( std::max( rowHeight * view->model()->rowCount() + 2 * frameWidth, rowHeight * 6 ), viewMaxSize.height() ) );
// Position should be central over the editor area, so map to global from parent of central widget since
// the view is positioned in global coords
QPoint centralWidgetPos = window->mapToGlobal( window->centralWidget()->pos() );
const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width() ) / 2);
const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height() ) / 2);
view->setFixedSize(viewSize);
view->move(xPos, yPos);
}
void DocumentSwitcherPlugin::walk(const int from, const int to)
{
auto* window = qobject_cast<Sublime::MainWindow*>( KDevelop::ICore::self()->uiController()->activeMainWindow() );
QModelIndex idx;
const int step = from < to ? 1 : -1;
if(!view->isVisible())
{
fillModel();
setViewGeometry(window);
idx = model->index(from + step, 0);
if(!idx.isValid()) { idx = model->index(0, 0); }
view->show();
} else {
int newRow = view->selectionModel()->currentIndex().row() + step;
if(newRow == to + step) { newRow = from; }
idx = model->index(newRow, 0);
}
view->selectionModel()->select(idx, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
view->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
}
void DocumentSwitcherPlugin::walkForward() { walk(0, model->rowCount()-1); }
void DocumentSwitcherPlugin::walkBackward() { walk(model->rowCount()-1, 0); }
void DocumentSwitcherPlugin::fillModel()
{
model->clear();
for (auto *doc : documentLists) {
DocumentSwitcherItem *item = new DocumentSwitcherItem(doc);
model->appendRow(item);
}
}
DocumentSwitcherPlugin::~DocumentSwitcherPlugin()
{
}
void DocumentSwitcherPlugin::switchToClicked( const QModelIndex& idx )
{
view->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
itemActivated(idx);
}
void DocumentSwitcherPlugin::itemActivated( const QModelIndex& idx )
{
Q_UNUSED( idx );
if( view->selectionModel()->selectedRows().isEmpty() )
{
return;
}
const int row = view->selectionModel()->selectedRows().first().row();
// Retrieve document from index
auto* activatedDocument = documentLists.value(row);
if( activatedDocument ) {
// Close document
if( QApplication::mouseButtons() & Qt::MiddleButton )
{
activatedDocument->close();
fillModel();
if ( model->rowCount() == 0 ) {
view->hide();
} else {
view->selectionModel()->select( view->model()->index(0, 0), QItemSelectionModel::ClearAndSelect );
}
}
// Activate document
else
{
KDevelop::IDocumentController *documentController = KDevelop::ICore::self()->documentController();
documentController->activateDocument(activatedDocument);
view->hide();
}
}
}
void DocumentSwitcherPlugin::documentOpened(KDevelop::IDocument *document)
{
if (!documentLists.contains(document)) {
documentLists.prepend(document);
}
}
void DocumentSwitcherPlugin::documentActivated(KDevelop::IDocument *document)
{
documentLists.removeOne(document);
documentLists.prepend(document);
}
void DocumentSwitcherPlugin::documentClosed(KDevelop::IDocument *document)
{
documentLists.removeOne(document);
}
void DocumentSwitcherPlugin::unload()
{
delete forwardAction;
delete backwardAction;
view->deleteLater();
}
bool DocumentSwitcherPlugin::eventFilter( QObject* watched, QEvent* ev )
{
auto* mw = qobject_cast<Sublime::MainWindow*>(watched);
if( mw && ev->type() == QEvent::WindowActivate )
{
enableActions();
}
return QObject::eventFilter( watched, ev );
}
void DocumentSwitcherPlugin::enableActions()
{
forwardAction->setEnabled(true);
backwardAction->setEnabled(true);
}
#include "documentswitcherplugin.moc"
#include "moc_documentswitcherplugin.cpp"
|