Commit 71c637ff authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

kateview*: Use more std containers and cleanup


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent ace4d9d6
......@@ -36,7 +36,7 @@
// END Includes
static const qint64 FileSizeAboveToAskUserIfProceedWithOpen = 10 * 1024 * 1024; // 10MB should suffice
static constexpr qint64 FileSizeAboveToAskUserIfProceedWithOpen = 10 * 1024 * 1024; // 10MB should suffice
KateViewManager::KateViewManager(QWidget *parentW, KateMainWindow *parent)
: QSplitter(parentW)
......@@ -59,7 +59,7 @@ KateViewManager::KateViewManager(QWidget *parentW, KateMainWindow *parent)
addWidget(vs);
vs->setActive(true);
m_viewSpaceList.append(vs);
m_viewSpaceList.push_back(vs);
connect(this, &KateViewManager::viewChanged, this, &KateViewManager::slotViewChanged);
......@@ -194,11 +194,11 @@ void KateViewManager::setupActions()
void KateViewManager::updateViewSpaceActions()
{
m_closeView->setEnabled(m_viewSpaceList.count() > 1);
m_closeOtherViews->setEnabled(m_viewSpaceList.count() > 1);
m_toggleSplitterOrientation->setEnabled(m_viewSpaceList.count() > 1);
goNext->setEnabled(m_viewSpaceList.count() > 1);
goPrev->setEnabled(m_viewSpaceList.count() > 1);
m_closeView->setEnabled(m_viewSpaceList.size() > 1);
m_closeOtherViews->setEnabled(m_viewSpaceList.size() > 1);
m_toggleSplitterOrientation->setEnabled(m_viewSpaceList.size() > 1);
goNext->setEnabled(m_viewSpaceList.size() > 1);
goPrev->setEnabled(m_viewSpaceList.size() > 1);
}
void KateViewManager::slotDocumentNew()
......@@ -270,11 +270,12 @@ void KateViewManager::slotDocumentClose(KTextEditor::Document *document)
void KateViewManager::slotDocumentClose()
{
// no active view, do nothing
if (!activeView()) {
auto view = activeView();
if (!view) {
return;
}
slotDocumentClose(activeView()->document());
slotDocumentClose(view->document());
}
KTextEditor::Document *KateViewManager::openUrl(const QUrl &url, const QString &encoding, bool activate, bool isTempFile, const KateDocumentInfo &docInfo)
......@@ -315,9 +316,7 @@ KTextEditor::View *KateViewManager::openUrlWithView(const QUrl &url, const QStri
m_mainWindow->addRecentOpenedFile(doc->url());
activateView(doc);
return activeView();
return activateView(doc);
}
void KateViewManager::openUrl(const QUrl &url)
......@@ -349,16 +348,17 @@ void KateViewManager::documentCreated(KTextEditor::Document *doc)
return;
}
if (!activeView()) {
activateView(doc);
auto view = activeView();
if (!view) {
view = activateView(doc);
}
/**
* check if we have any empty viewspaces and give them a view
*/
for (KateViewSpace *vs : qAsConst(m_viewSpaceList)) {
for (KateViewSpace *vs : m_viewSpaceList) {
if (!vs->currentView()) {
createView(activeView()->document(), vs);
createView(view->document(), vs);
}
}
}
......@@ -399,7 +399,7 @@ void KateViewManager::documentsDeleted(const QList<KTextEditor::Document *> &)
/**
* check if we have any empty viewspaces and give them a view
*/
for (KateViewSpace *vs : qAsConst(m_viewSpaceList)) {
for (KateViewSpace *vs : m_viewSpaceList) {
if (!vs->currentView()) {
createView(newActiveView->document(), vs);
}
......@@ -511,23 +511,23 @@ bool KateViewManager::deleteView(KTextEditor::View *view)
}
// remove view from mapping and memory !!
m_views.remove(view);
m_views.erase(view);
delete view;
return true;
}
KateViewSpace *KateViewManager::activeViewSpace()
{
for (QList<KateViewSpace *>::const_iterator it = m_viewSpaceList.constBegin(); it != m_viewSpaceList.constEnd(); ++it) {
if ((*it)->isActiveSpace()) {
return *it;
for (auto vs : m_viewSpaceList) {
if (vs->isActiveSpace()) {
return vs;
}
}
// none active, so use the first we grab
if (!m_viewSpaceList.isEmpty()) {
m_viewSpaceList.first()->setActive(true);
return m_viewSpaceList.first();
if (!m_viewSpaceList.empty()) {
m_viewSpaceList.front()->setActive(true);
return m_viewSpaceList.front();
}
Q_ASSERT(false);
......@@ -542,12 +542,10 @@ KTextEditor::View *KateViewManager::activeView()
m_activeViewRunning = true;
QHashIterator<KTextEditor::View *, ViewData> it(m_views);
while (it.hasNext()) {
it.next();
if (it.value().active) {
for (const auto &[view, viewData] : m_views) {
if (viewData.active) {
m_activeViewRunning = false;
return it.key();
return view;
}
}
......@@ -563,7 +561,7 @@ KTextEditor::View *KateViewManager::activeView()
// last attempt: pick MRU view
auto views = sortedViews();
if (!views.isEmpty()) {
if (!views.empty()) {
KTextEditor::View *v = views.front();
activateView(v);
m_activeViewRunning = false;
......@@ -578,8 +576,8 @@ KTextEditor::View *KateViewManager::activeView()
void KateViewManager::setActiveSpace(KateViewSpace *vs)
{
if (activeViewSpace()) {
activeViewSpace()->setActive(false);
if (auto activeSpace = activeViewSpace()) {
activeSpace->setActive(false);
}
vs->setActive(true);
......@@ -591,8 +589,8 @@ void KateViewManager::setActiveSpace(KateViewSpace *vs)
void KateViewManager::setActiveView(KTextEditor::View *view)
{
if (activeView()) {
m_views[activeView()].active = false;
if (auto v = activeView()) {
m_views[v].active = false;
}
if (view) {
......@@ -629,8 +627,11 @@ void KateViewManager::activateView(KTextEditor::View *view)
return;
}
Q_ASSERT(m_views.contains(view));
if (!m_views[view].active) {
auto it = m_views.find(view);
Q_ASSERT(it != m_views.end());
ViewData &viewData = it->second;
if (!viewData.active) {
// avoid flicker
KateUpdateDisabler disableUpdates(mainWindow());
......@@ -662,7 +663,7 @@ void KateViewManager::activateView(KTextEditor::View *view)
}
// remember age of this view
m_views[view].lruAge = m_minAge--;
viewData.lruAge = m_minAge--;
Q_EMIT viewChanged(view);
......@@ -682,8 +683,9 @@ KTextEditor::View *KateViewManager::activateView(KTextEditor::Document *d)
}
// activate existing view if possible
if (activeViewSpace()->showView(d)) {
activateView(activeViewSpace()->currentView());
auto activeSpace = activeViewSpace();
if (activeSpace->showView(d)) {
activateView(activeSpace->currentView());
return activeView();
}
......@@ -694,33 +696,36 @@ KTextEditor::View *KateViewManager::activateView(KTextEditor::Document *d)
void KateViewManager::slotViewChanged()
{
if (activeView() && !activeView()->hasFocus()) {
activeView()->setFocus();
auto view = activeView();
if (view && !view->hasFocus()) {
view->setFocus();
}
}
void KateViewManager::activateNextView()
{
int i = m_viewSpaceList.indexOf(activeViewSpace()) + 1;
auto it = std::find(m_viewSpaceList.begin(), m_viewSpaceList.end(), activeViewSpace());
++it;
if (i >= m_viewSpaceList.count()) {
i = 0;
if (it == m_viewSpaceList.end()) {
it = m_viewSpaceList.begin();
}
setActiveSpace(m_viewSpaceList.at(i));
activateView(m_viewSpaceList.at(i)->currentView());
setActiveSpace(*it);
activateView((*it)->currentView());
}
void KateViewManager::activatePrevView()
{
int i = m_viewSpaceList.indexOf(activeViewSpace()) - 1;
if (i < 0) {
i = m_viewSpaceList.count() - 1;
auto it = std::find(m_viewSpaceList.begin(), m_viewSpaceList.end(), activeViewSpace());
if (it == m_viewSpaceList.begin()) {
it = --m_viewSpaceList.end();
} else {
--it;
}
setActiveSpace(m_viewSpaceList.at(i));
activateView(m_viewSpaceList.at(i)->currentView());
setActiveSpace(*it);
activateView((*it)->currentView());
}
void KateViewManager::documentWillBeDeleted(KTextEditor::Document *doc)
......@@ -731,7 +736,7 @@ void KateViewManager::documentWillBeDeleted(KTextEditor::Document *doc)
QList<KTextEditor::View *> closeList;
const auto views = doc->views();
for (KTextEditor::View *v : views) {
if (m_views.contains(v)) {
if (m_views.find(v) != m_views.end()) {
closeList.append(v);
}
}
......@@ -762,7 +767,7 @@ void KateViewManager::closeView(KTextEditor::View *view)
/**
* check if we have any empty viewspaces and give them a view
*/
for (KateViewSpace *vs : qAsConst(m_viewSpaceList)) {
for (KateViewSpace *vs : m_viewSpaceList) {
if (!vs->currentView()) {
createView(newActiveView->document(), vs);
}
......@@ -832,7 +837,7 @@ void KateViewManager::splitViewSpace(KateViewSpace *vs, // = 0
newContainer->setSizes(newSizes);
}
m_viewSpaceList.append(vsNew);
m_viewSpaceList.push_back(vsNew);
activeViewSpace()->setActive(false);
vsNew->setActive(true);
vsNew->show();
......@@ -900,7 +905,7 @@ void KateViewManager::removeViewSpace(KateViewSpace *viewspace)
}
// abort if this is the last viewspace
if (m_viewSpaceList.count() < 2) {
if (m_viewSpaceList.size() < 2) {
return;
}
......@@ -930,7 +935,7 @@ void KateViewManager::removeViewSpace(KateViewSpace *viewspace)
}
// cu viewspace
m_viewSpaceList.removeAt(m_viewSpaceList.indexOf(viewspace));
m_viewSpaceList.erase(std::remove(m_viewSpaceList.begin(), m_viewSpaceList.end(), viewspace), m_viewSpaceList.end());
delete viewspace;
// at this point, the splitter has exactly 1 child
......@@ -966,12 +971,13 @@ void KateViewManager::removeViewSpace(KateViewSpace *viewspace)
}
// add the known documents to the current view space to not loose tab buttons
auto avs = activeViewSpace();
for (auto doc : documentList) {
activeViewSpace()->registerDocument(doc);
avs->registerDocument(doc);
}
// find the view that is now active.
KTextEditor::View *v = activeViewSpace()->currentView();
KTextEditor::View *v = avs->currentView();
if (v) {
activateView(v);
}
......@@ -1001,7 +1007,7 @@ void KateViewManager::slotHideOtherViews(bool hideOthers)
KateUpdateDisabler disableUpdates(mainWindow());
const KateViewSpace *active = activeViewSpace();
for (KateViewSpace *v : qAsConst(m_viewSpaceList)) {
for (KateViewSpace *v : m_viewSpaceList) {
if (active != v) {
v->setVisible(!hideOthers);
}
......@@ -1064,7 +1070,7 @@ void KateViewManager::restoreViewConfiguration(const KConfigGroup &config)
restoreSplitter(config.config(), config.name() + QStringLiteral("-Splitter 0"), this, config.name());
// finally, make the correct view from the last session active
int lastViewSpace = config.readEntry("Active ViewSpace", 0);
size_t lastViewSpace = config.readEntry("Active ViewSpace", 0);
if (lastViewSpace > m_viewSpaceList.size()) {
lastViewSpace = 0;
}
......@@ -1086,7 +1092,7 @@ void KateViewManager::restoreViewConfiguration(const KConfigGroup &config)
KateViewSpace *vs = new KateViewSpace(this, nullptr);
addWidget(vs);
vs->setActive(true);
m_viewSpaceList.append(vs);
m_viewSpaceList.push_back(vs);
/**
* activate at least one document!
......@@ -1128,12 +1134,15 @@ QString KateViewManager::saveSplitterConfig(QSplitter *s, KConfigBase *configBas
// For KateViewSpaces, ask them to save the file list.
auto obj = s->widget(it);
if (auto kvs = qobject_cast<KateViewSpace *>(obj)) {
childList.append(QString(viewConfGrp + QStringLiteral("-ViewSpace %1")).arg(m_viewSpaceList.indexOf(kvs)));
kvs->saveConfig(configBase, m_viewSpaceList.indexOf(kvs), viewConfGrp);
auto it = std::find(m_viewSpaceList.begin(), m_viewSpaceList.end(), kvs);
int idx = (int)std::distance(m_viewSpaceList.begin(), it);
childList.append(QString(viewConfGrp + QStringLiteral("-ViewSpace %1")).arg(idx));
kvs->saveConfig(configBase, idx, viewConfGrp);
// save active viewspace
if (kvs->isActiveSpace()) {
KConfigGroup viewConfGroup(configBase, viewConfGrp);
viewConfGroup.writeEntry("Active ViewSpace", m_viewSpaceList.indexOf(kvs));
viewConfGroup.writeEntry("Active ViewSpace", idx);
}
}
// for QSplitters, recurse
......@@ -1166,7 +1175,7 @@ void KateViewManager::restoreSplitter(const KConfigBase *configBase, const QStri
// for a viewspace, create it and open all documents therein.
if (str.startsWith(viewConfGrp + QStringLiteral("-ViewSpace"))) {
KateViewSpace *vs = new KateViewSpace(this, nullptr);
m_viewSpaceList.append(vs);
m_viewSpaceList.push_back(vs);
// make active so that the view created in restoreConfig has this
// new view space as parent.
setActiveSpace(vs);
......
......@@ -16,6 +16,8 @@
#include <QPointer>
#include <QSplitter>
#include <unordered_map>
namespace KActivities
{
class ResourceInstance;
......@@ -248,22 +250,38 @@ public Q_SLOTS:
*/
QList<KTextEditor::View *> views() const
{
return m_views.keys();
QList<KTextEditor::View *> ret;
ret.reserve(m_views.size());
std::transform(m_views.begin(), m_views.end(), std::back_inserter(ret), [](const std::pair<KTextEditor::View *, ViewData> &p) {
return p.first;
});
return ret;
}
/**
* get views in lru order
* @return views in lru order
*/
QList<KTextEditor::View *> sortedViews() const
std::vector<KTextEditor::View *> sortedViews() const
{
QMap<qint64, KTextEditor::View *> sortedViews;
QHashIterator<KTextEditor::View *, ViewData> i(m_views);
while (i.hasNext()) {
i.next();
sortedViews[i.value().lruAge] = i.key();
}
return sortedViews.values();
std::vector<std::pair<KTextEditor::View *, qint64>> sorted;
// extract into a list
std::transform(m_views.begin(), m_views.end(), std::back_inserter(sorted), [](const std::pair<KTextEditor::View *, ViewData> &p) {
return std::pair<KTextEditor::View *, qint64>{p.first, p.second.lruAge};
});
// sort the views based on lru
std::sort(sorted.begin(), sorted.end(), [](const std::pair<KTextEditor::View *, qint64> &l, const std::pair<KTextEditor::View *, qint64> &r) {
return l.second < r.second;
});
// extract the views only and return
std::vector<KTextEditor::View *> ret;
ret.reserve(sorted.size());
std::transform(sorted.begin(), sorted.end(), std::back_inserter(ret), [](const std::pair<KTextEditor::View *, qint64> &p) {
return p.first;
});
return ret;
}
private:
......@@ -279,7 +297,7 @@ private:
QAction *goNext = nullptr;
QAction *goPrev = nullptr;
QList<KateViewSpace *> m_viewSpaceList;
std::vector<KateViewSpace *> m_viewSpaceList;
bool m_blockViewCreationAndActivation;
......@@ -293,11 +311,6 @@ private:
class ViewData
{
public:
/**
* Default constructor
*/
ViewData() = default;
/**
* view active?
*/
......@@ -319,7 +332,7 @@ private:
* central storage of all views known in the view manager
* maps the view to meta data
*/
QHash<KTextEditor::View *, ViewData> m_views;
std::unordered_map<KTextEditor::View *, ViewData> m_views;
/**
* current minimal age
......
......@@ -171,7 +171,7 @@ bool KateViewSpace::eventFilter(QObject *obj, QEvent *event)
void KateViewSpace::statusBarToggled()
{
KateUpdateDisabler updatesDisabled(m_viewManager->mainWindow());
for (auto view : qAsConst(m_docToView)) {
for (const auto [doc, view] : m_docToView) {
view->setStatusBarEnabled(m_viewManager->mainWindow()->showStatusBar());
}
}
......@@ -189,7 +189,10 @@ void KateViewSpace::tabBarToggled()
KTextEditor::View *KateViewSpace::createView(KTextEditor::Document *doc)
{
// should only be called if a view does not yet exist
Q_ASSERT(!m_docToView.contains(doc));
{
auto it = m_docToView.find(doc);
Q_ASSERT(it == m_docToView.end());
}
/**
* Create a fresh view
......@@ -222,7 +225,7 @@ KTextEditor::View *KateViewSpace::createView(KTextEditor::Document *doc)
registerDocument(doc);
// view shall still be not registered
Q_ASSERT(!m_docToView.contains(doc));
Q_ASSERT(m_docToView.find(doc) == m_docToView.end());
// insert View into stack
stack->addWidget(v);
......@@ -235,17 +238,19 @@ KTextEditor::View *KateViewSpace::createView(KTextEditor::Document *doc)
void KateViewSpace::removeView(KTextEditor::View *v)
{
// remove view mappings
Q_ASSERT(m_docToView.contains(v->document()));
m_docToView.remove(v->document());
auto it = m_docToView.find(v->document());
Q_ASSERT(it != m_docToView.end());
m_docToView.erase(it);
// ...and now: remove from view space
stack->removeWidget(v);
// switch to most recently used rather than letting stack choose one
// (last element could well be v->document() being removed here)
for (auto it = m_registeredDocuments.rbegin(); it != m_registeredDocuments.rend(); ++it) {
if (m_docToView.contains(*it)) {
showView(*it);
for (auto rit = m_registeredDocuments.rbegin(); rit != m_registeredDocuments.rend(); ++rit) {
auto it = m_docToView.find(*rit);
if (it != m_docToView.end()) {
showView(*rit);
break;
}
}
......@@ -256,7 +261,8 @@ bool KateViewSpace::showView(KTextEditor::Document *document)
/**
* nothing can be done if we have now view ready here
*/
if (!m_docToView.contains(document)) {
auto it = m_docToView.find(document);
if (it == m_docToView.end()) {
return false;
}
......@@ -275,7 +281,7 @@ bool KateViewSpace::showView(KTextEditor::Document *document)
/**
* show the wanted view
*/
KTextEditor::View *kv = m_docToView[document];
KTextEditor::View *kv = it->second;
stack->setCurrentWidget(kv);
kv->show();
......@@ -396,7 +402,7 @@ void KateViewSpace::documentDestroyed(QObject *doc)
/**
* we shall have no views for this document at this point in time!
*/
Q_ASSERT(!m_docToView.contains(invalidDoc));
Q_ASSERT(m_docToView.find(invalidDoc) == m_docToView.end());
// disconnect entirely
disconnect(doc, nullptr, this, nullptr);
......@@ -484,18 +490,18 @@ void KateViewSpace::addPositionToHistory(const QUrl &url, KTextEditor::Cursor c,
}
// we are in the middle of jumps somewhere?
if (!m_locations.isEmpty() && currentLocation + 1 < m_locations.size()) {
if (!m_locations.empty() && currentLocation + 1 < m_locations.size()) {
// erase all forward history
m_locations.erase(m_locations.begin() + currentLocation + 1, m_locations.end());
}
// if same line, remove last entry
if (!m_locations.isEmpty() && m_locations.back().url == url && m_locations.back().cursor.line() == c.line()) {
if (!m_locations.empty() && m_locations.back().url == url && m_locations.back().cursor.line() == c.line()) {
m_locations.pop_back();
}
// Check if the location is at least "viewLineCount" away
if (!calledExternally && !m_locations.isEmpty() && m_locations.back().url == url) {
if (!calledExternally && !m_locations.empty() && m_locations.back().url == url) {
int line = c.line();
int lastLocLine = m_locations.back().cursor.line();
......@@ -636,8 +642,9 @@ void KateViewSpace::saveConfig(KConfigBase *config, int myIndex, const QString &
const auto docList = documentList();
for (KTextEditor::Document *doc : docList) {
lruList << doc->url().toString();
if (m_docToView.contains(doc)) {
views.push_back(m_docToView[doc]);
auto it = m_docToView.find(doc);
if (it != m_docToView.end()) {
views.push_back(it->second);
}
}
......@@ -718,8 +725,8 @@ void KateViewSpace::restoreConfig(KateViewManager *viewMan, const KConfigBase *c
}
// avoid empty view space
if (m_docToView.isEmpty()) {
auto *doc = KateApp::self()->documentManager()->documentList().front();
if (m_docToView.empty()) {
auto *doc = KateApp::self()->documentManager()->documentList().first();
if (!fn.isEmpty()) {
QUrl url(fn);
KateApp::self()->documentManager()->documentInfo(doc)->doPostLoadOperations =
......@@ -733,7 +740,7 @@ void KateViewSpace::restoreConfig(KateViewManager *viewMan, const KConfigBase *c
void KateViewSpace::goBack()
{
if (m_locations.isEmpty() || currentLocation == 0) {
if (m_locations.empty() || currentLocation == 0) {
return;
}
......@@ -747,8 +754,8 @@ void KateViewSpace::goBack()
if (auto v = m_viewManager->activeView()) {
if (v->document() && v->document()->url() == location.url) {
const QSignalBlocker blocker(m_viewManager->activeView());
m_viewManager->activeView()->setCursorPosition(location.cursor);
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
// enable forward
m_historyForward->setEnabled(true);
Q_EMIT m_viewManager->historyForwardEnabled(true);
......@@ -776,7 +783,7 @@ bool KateViewSpace::isHistoryForwardEnabled() const
void KateViewSpace::goForward()
{
if (m_locations.isEmpty()) {
if (m_locations.empty()) {
return;
}
if (currentLocation == m_locations.size() - 1) {
......@@ -792,7 +799,7 @@ void KateViewSpace::goForward()
}
if (!location.url.isValid() || !location.cursor.isValid()) {
m_locations.remove(currentLocation);
m_locations.erase(m_locations.begin() + currentLocation);
return;
}
......@@ -801,8 +808,8 @@ void KateViewSpace::goForward()
if (auto v = m_viewManager->activeView()) {
if (v->document() && v->document()->url() == location.url) {
const QSignalBlocker blocker(m_viewManager->activeView());
m_viewManager->activeView()->setCursorPosition(location.cursor);
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
return;
}
}
......