Commit cc340e87 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

LRU sorted document switching

will keep track on view activation
the CTRL-ALT-O list is LRU sorted, latest used doc first
pre-seletected is the previous used doc
just cursor down to broswe in LRU history order
we can't have CTRL-ALT-LEFT/RIGHT do that without such a dialog, otherwise you just would
swap between the latest 2 documents, as as soon as the next one is activated, the last
used one will be next in lru stack
BUG: 104997
parent cf8c8104
......@@ -20,6 +20,7 @@
#include "katequickopen.h"
#include "katequickopen.moc"
#include "katemainwindow.h"
#include "kateviewmanager.h"
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
......@@ -57,7 +58,6 @@ KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow)
layout->setMargin(0);
setLayout (layout);
m_inputLine = new KLineEdit();
setFocusProxy (m_inputLine);
m_inputLine->setClickMessage (i18n ("Quick Open Search"));
......@@ -69,7 +69,7 @@ KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow)
m_listView->setTextElideMode(Qt::ElideLeft);
m_base_model = new QStandardItemModel(0, 2, this);
m_model = new QSortFilterProxyModel(this);
m_model->setFilterRole(SortFilterRole);
m_model->setSortRole(SortFilterRole);
......@@ -90,25 +90,6 @@ KateQuickOpen::KateQuickOpen(QWidget *parent, KateMainWindow *mainWindow)
m_listView->installEventFilter(this);
m_listView->setHeaderHidden(true);
m_listView->setRootIsDecorated(false);
/**
* track view changes
*/
connect (m_mainWindow->mainWindow(), SIGNAL(viewChanged()), SLOT(slotViewChanged()));
}
void KateQuickOpen::slotViewChanged()
{
if (!m_mainWindow->mainWindow()->activeView())
return;
// when view changes update active and previous documents
// so that right document will be pre-selected on next quick-switch
KTextEditor::Document *newDoc = m_mainWindow->mainWindow()->activeView()->document();
if(newDoc != m_activeDoc) {
m_prevDoc = m_activeDoc;
m_activeDoc = newDoc;
}
}
bool KateQuickOpen::eventFilter(QObject *obj, QEvent *event)
......@@ -124,7 +105,7 @@ bool KateQuickOpen::eventFilter(QObject *obj, QEvent *event)
QCoreApplication::sendEvent(m_listView, event);
return true;
}
if (keyEvent->key() == Qt::Key_Escape) {
m_mainWindow->slotWindowActivated ();
m_inputLine->clear ();
......@@ -157,16 +138,70 @@ void KateQuickOpen::update ()
* new base mode creation
*/
QStandardItemModel *base_model = new QStandardItemModel(0, 2, this);
/**
* get all open documents
* remember local file names to avoid dupes with project files
*/
QSet<QString> alreadySeenFiles;
QSet<KTextEditor::Document *> alreadySeenDocs;
/**
* get views in lru order
*/
QMap<qint64, KTextEditor::View *> sortedViews;
QHashIterator<KTextEditor::View *, qint64> i(m_mainWindow->viewManager()->lruViews());
while (i.hasNext()) {
i.next ();
sortedViews[i.value()] = i.key();
}
/**
* now insert them in order
*/
QModelIndex idxToSelect;
int linecount = 0;
QMapIterator<qint64, KTextEditor::View *> i2(sortedViews);
while (i2.hasNext()) {
i2.next();
KTextEditor::Document *doc = i2.value()->document();
alreadySeenDocs.insert (doc);
//QStandardItem *item=new QStandardItem(i18n("%1: %2",doc->documentName(),doc->url().pathOrUrl()));
QStandardItem *itemName = new QStandardItem(doc->documentName());
itemName->setData(qVariantFromValue(QPointer<KTextEditor::Document> (doc)), DocumentRole);
itemName->setData(QString("%1: %2").arg(doc->documentName()).arg(doc->url().pathOrUrl()), SortFilterRole);
itemName->setEditable(false);
QFont font = itemName->font();
font.setBold(true);
itemName->setFont(font);
QStandardItem *itemUrl = new QStandardItem(doc->url().pathOrUrl());
itemUrl->setEditable(false);
base_model->setItem(linecount, 0, itemName);
base_model->setItem(linecount, 1, itemUrl);
linecount++;
if (!doc->url().isEmpty() && doc->url().isLocalFile())
alreadySeenFiles.insert (doc->url().toLocalFile());
// select second document, that is the last used (beside the active one)
if (linecount == 2)
idxToSelect = itemName->index();
}
/**
* get all open documents
*/
QList<KTextEditor::Document*> docs = Kate::application()->documentManager()->documents();
int linecount = 0;
QModelIndex idxToSelect;
foreach(KTextEditor::Document *doc, docs) {
/**
* skip docs already open
*/
if (alreadySeenDocs.contains (doc))
continue;
//QStandardItem *item=new QStandardItem(i18n("%1: %2",doc->documentName(),doc->url().pathOrUrl()));
QStandardItem *itemName = new QStandardItem(doc->documentName());
......@@ -182,12 +217,9 @@ void KateQuickOpen::update ()
base_model->setItem(linecount, 0, itemName);
base_model->setItem(linecount, 1, itemUrl);
linecount++;
if (!doc->url().isEmpty() && doc->url().isLocalFile())
alreadySeenFiles.insert (doc->url().toLocalFile());
if (doc == m_prevDoc)
idxToSelect = itemName->index();
}
/**
......@@ -201,7 +233,7 @@ void KateQuickOpen::update ()
*/
if (alreadySeenFiles.contains (file))
continue;
QFileInfo fi (file);
QStandardItem *itemName = new QStandardItem(fi.fileName());
......@@ -219,23 +251,23 @@ void KateQuickOpen::update ()
linecount++;
}
}
/**
* swap models and kill old one
*/
m_model->setSourceModel (base_model);
delete m_base_model;
m_base_model = base_model;
if(idxToSelect.isValid())
m_listView->setCurrentIndex(m_model->mapFromSource(idxToSelect));
else
reselectFirst();
/**
* adjust view
*/
m_listView->resizeColumnToContents(0);
m_listView->resizeColumnToContents(0);
}
void KateQuickOpen::slotReturnPressed ()
......@@ -252,7 +284,7 @@ void KateQuickOpen::slotReturnPressed ()
if (!url.isEmpty())
m_mainWindow->mainWindow()->openUrl (url);
}
/**
* in any case, switch back to view manager
*/
......
......@@ -49,9 +49,10 @@ class KateQuickOpen : public QWidget {
* will fill model with current open documents, project documents, ...
*/
void update ();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private Q_SLOTS:
void reselectFirst();
......@@ -60,36 +61,21 @@ class KateQuickOpen : public QWidget {
* and go back to background
*/
void slotReturnPressed ();
/**
* Track view changes
*/
void slotViewChanged();
private:
KateMainWindow *m_mainWindow;
QTreeView *m_listView;
KLineEdit *m_inputLine;
/**
* our model we search in
*/
QStandardItemModel *m_base_model;
/**
* filtered model we search in
*/
QSortFilterProxyModel *m_model;
/**
* previous active document
*/
QPointer<KTextEditor::Document> m_prevDoc;
/**
* current active document
*/
QPointer<KTextEditor::Document> m_activeDoc;
};
#endif
......@@ -70,6 +70,7 @@ KateViewManager::KateViewManager (QWidget *parentW, KateMainWindow *parent)
, m_mainWindow(parent)
, m_blockViewCreationAndActivation (false)
, m_activeViewRunning (false)
, m_minAge (0)
{
// while init
m_init = true;
......@@ -435,6 +436,9 @@ bool KateViewManager::deleteView (KTextEditor::View *view, bool delViewSpace)
#ifdef KActivities_FOUND
m_activityResources.remove(view);
#endif
// kill LRU mapping
m_lruViews.remove (view);
// remove view from list and memory !!
m_viewList.removeAt ( m_viewList.indexOf (view) );
......@@ -589,6 +593,9 @@ void KateViewManager::activateView ( KTextEditor::View *view )
mainWindow()->toolBar()->show();
mainWindow()->setUpdatesEnabled( true );
// remember age of this view
m_lruViews[view] = m_minAge--;
emit viewChanged();
}
}
......@@ -837,6 +844,7 @@ void KateViewManager::restoreViewConfiguration (const KConfigGroup& config)
qDeleteAll( m_viewSpaceList );
m_viewSpaceList.clear();
m_activeStates.clear();
m_lruViews.clear();
// start recursion for the root splitter (Splitter 0)
restoreSplitter( config.config(), config.name() + "-Splitter 0", this, config.name() );
......
......@@ -246,6 +246,16 @@ class KateViewManager : public QSplitter
void slotCloseCurrentViewSpace();
void reactivateActiveView();
/**
* get views => age mapping
* useful to show views in a LRU way
* important: smallest age ==> latest used view
*/
const QHash<KTextEditor::View *, qint64> &lruViews () const
{
return m_lruViews;
}
private:
KateMainWindow *m_mainWindow;
......@@ -274,6 +284,17 @@ class KateViewManager : public QSplitter
bool m_activeViewRunning;
int m_splitterIndex; // used during saving splitter config.
/**
* history of view activations
* map view => number, the lower, the more recent activated
*/
QHash<KTextEditor::View *, qint64> m_lruViews;
/**
* current minimal age
*/
qint64 m_minAge;
};
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment