[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdegraphics/gwenview/app
From:       Aurélien Gâteau <agateau () kde ! org>
Date:       2010-11-13 23:58:17
Message-ID: 20101113235817.60BA7AC8A5 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1196735 by gateau:

ContextManager now knows about the selection model:

- correctly update the sidebar when current url is renamed
- a bit less logic code in MainWindow

 M  +91 -28    contextmanager.cpp  
 M  +5 -4      contextmanager.h  
 M  +2 -0      infocontextmanageritem.cpp  
 M  +3 -52     mainwindow.cpp  
 M  +0 -6      mainwindow.h  


--- trunk/KDE/kdegraphics/gwenview/app/contextmanager.cpp #1196734:1196735
@@ -20,9 +20,11 @@
 #include "contextmanager.moc"
 
 // Qt
+#include <QItemSelectionModel>
 #include <QTimer>
 
 // KDE
+#include <kdebug.h>
 #include <kfileitem.h>
 
 // Local
@@ -34,30 +36,64 @@
 
 struct ContextManagerPrivate {
 	QList<AbstractContextManagerItem*> mList;
-	KFileItemList mSelection;
 	SortedDirModel* mDirModel;
+	QItemSelectionModel* mSelectionModel;
 	KUrl mCurrentDirUrl;
 	KUrl mCurrentUrl;
 
-	QTimer* mSelectionDataChangedTimer;
+	bool mSelectedFileItemListNeedsUpdate;
+	QSet<QByteArray> mQueuedSignals;
+	KFileItemList mSelectedFileItemList;
 
-	void scheduleEmittingSelectionDataChanged() {
-		mSelectionDataChangedTimer->start();
+	QTimer* mQueuedSignalsTimer;
+
+	void queueSignal(const QByteArray& signal) {
+		mQueuedSignals << signal;
+		mQueuedSignalsTimer->start();
 	}
+
+	void updateSelectedFileItemList() {
+		if (!mSelectedFileItemListNeedsUpdate) {
+			return;
+		}
+		mSelectedFileItemList.clear();
+		QItemSelection selection = mSelectionModel->selection();
+		Q_FOREACH(const QModelIndex& index, selection.indexes()) {
+			mSelectedFileItemList << mDirModel->itemForIndex(index);
+		}
+
+		// At least add current url if it's valid (it may not be in
+		// the list if we are viewing a non-browsable url, for example
+		// using http protocol)
+		if (mSelectedFileItemList.isEmpty() && mCurrentUrl.isValid()) {
+			KFileItem item(KFileItem::Unknown, KFileItem::Unknown, mCurrentUrl);
+			mSelectedFileItemList << item;
+		}
+
+		mSelectedFileItemListNeedsUpdate = false;
+	}
 };
 
 
-ContextManager::ContextManager(QObject* parent)
+ContextManager::ContextManager(SortedDirModel* dirModel, QItemSelectionModel* \
selectionModel, QObject* parent)  : QObject(parent)
 , d(new ContextManagerPrivate)
 {
-	d->mSelectionDataChangedTimer = new QTimer(this);
-	d->mSelectionDataChangedTimer->setInterval(500);
-	d->mSelectionDataChangedTimer->setSingleShot(true);
-	connect(d->mSelectionDataChangedTimer, SIGNAL(timeout()),
-		SIGNAL(selectionDataChanged()) );
+	d->mQueuedSignalsTimer = new QTimer(this);
+	d->mQueuedSignalsTimer->setInterval(100);
+	d->mQueuedSignalsTimer->setSingleShot(true);
+	connect(d->mQueuedSignalsTimer, SIGNAL(timeout()),
+		SLOT(emitQueuedSignals()) );
 
-	d->mDirModel = 0;
+	d->mDirModel = dirModel;
+	connect(d->mDirModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
+		SLOT(slotDirModelDataChanged(const QModelIndex&, const QModelIndex&)) );
+
+	d->mSelectionModel = selectionModel;
+	connect(d->mSelectionModel, SIGNAL(selectionChanged(const QItemSelection&, const \
QItemSelection&)), +		SLOT(slotSelectionChanged()) );
+
+	d->mSelectedFileItemListNeedsUpdate = false;
 }
 
 
@@ -72,15 +108,17 @@
 }
 
 
-void ContextManager::setContext(const KUrl& currentUrl, const KFileItemList& \
selection) { +void ContextManager::setCurrentUrl(const KUrl& currentUrl) {
+	if (d->mCurrentUrl != currentUrl) {
 	d->mCurrentUrl = currentUrl;
-	d->mSelection = selection;
 	selectionChanged();
 }
+}
 
 
 KFileItemList ContextManager::selection() const {
-	return d->mSelection;
+	d->updateSelectedFileItemList();
+	return d->mSelectedFileItemList;
 }
 
 
@@ -108,27 +146,52 @@
 }
 
 
-void ContextManager::setDirModel(SortedDirModel* dirModel) {
-	d->mDirModel = dirModel;
+void ContextManager::slotDirModelDataChanged(const QModelIndex& topLeft, const \
QModelIndex& bottomRight) { +	// Data change can happen in the following cases:
+	// - items have been renamed
+	// - item bytes have been modified
+	// - item meta info has been retrieved or modified
+	//
+	// If a selected item is affected, schedule emission of a
+	// selectionDataChanged() signal. Don't emit it directly to avoid spamming
+	// the context items in case of a mass change.
+	QModelIndexList selectionList = d->mSelectionModel->selectedIndexes();
+	if (selectionList.isEmpty()) {
+		return;
+	}
 
-	connect(d->mDirModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
-		SLOT(slotDirModelDataChanged(const QModelIndex&, const QModelIndex&)) );
+	QModelIndexList changedList;
+	for (int row=topLeft.row(); row <= bottomRight.row(); ++row) {
+		changedList << d->mDirModel->index(row, 0);
 }
 
-
-void ContextManager::slotDirModelDataChanged(const QModelIndex& topLeft, const \
                QModelIndex& bottomRight) {
-	// Look if a selected item has changed, if there is one, schedule emission
-	// of a selectionDataChanged() signal. Don't emit it directly to avoid
-	// spamming the context items in case of a mass change.
-	for (int row=topLeft.row(); row <= bottomRight.row(); ++row) {
-		const QModelIndex index = d->mDirModel->index(row, 0);
-		const KFileItem item = d->mDirModel->itemForIndex(index);
-		if (d->mSelection.contains(item)) {
-			d->scheduleEmittingSelectionDataChanged();
+	QModelIndexList& shortList = selectionList;
+	QModelIndexList& longList = changedList;
+	if (shortList.length() > longList.length()) {
+		qSwap(shortList, longList);
+	}
+	Q_FOREACH(const QModelIndex& index, shortList) {
+		if (longList.contains(index)) {
+			d->mSelectedFileItemListNeedsUpdate = true;
+			d->queueSignal("selectionDataChanged");
 			return;
 		}
 	}
 }
 
 
+void ContextManager::slotSelectionChanged() {
+	d->mSelectedFileItemListNeedsUpdate = true;
+	d->queueSignal("selectionChanged");
+}
+
+
+void ContextManager::emitQueuedSignals() {
+	Q_FOREACH(const QByteArray& signal, d->mQueuedSignals) {
+		QMetaObject::invokeMethod(this, signal.data());
+	}
+	d->mQueuedSignals.clear();
+}
+
+
 } // namespace
--- trunk/KDE/kdegraphics/gwenview/app/contextmanager.h #1196734:1196735
@@ -27,6 +27,7 @@
 #include <kurl.h>
 #include <kfileitem.h>
 
+class QItemSelectionModel;
 class QModelIndex;
 
 namespace Gwenview {
@@ -44,7 +45,7 @@
 class ContextManager : public QObject {
 	Q_OBJECT
 public:
-	ContextManager(QObject* parent);
+	ContextManager(SortedDirModel*, QItemSelectionModel*, QObject* parent);
 
 	~ContextManager();
 
@@ -56,12 +57,10 @@
 
 	KUrl currentDirUrl() const;
 
-	void setContext(const KUrl& currentUrl, const KFileItemList& selection);
+	void setCurrentUrl(const KUrl& currentUrl);
 
 	KFileItemList selection() const;
 
-	void setDirModel(SortedDirModel*);
-
 	SortedDirModel* dirModel() const;
 
 Q_SIGNALS:
@@ -71,6 +70,8 @@
 
 private Q_SLOTS:
 	void slotDirModelDataChanged(const QModelIndex& topLeft, const QModelIndex& \
bottomRight); +	void slotSelectionChanged();
+	void emitQueuedSignals();
 
 private:
 	ContextManagerPrivate* const d;
--- trunk/KDE/kdegraphics/gwenview/app/infocontextmanageritem.cpp #1196734:1196735
@@ -233,6 +233,8 @@
 	d->setupGroup();
 	connect(contextManager(), SIGNAL(selectionChanged()),
 		SLOT(updateSideBarContent()) );
+	connect(contextManager(), SIGNAL(selectionDataChanged()),
+		SLOT(updateSideBarContent()) );
 }
 
 InfoContextManagerItem::~InfoContextManagerItem() {
--- trunk/KDE/kdegraphics/gwenview/app/mainwindow.cpp #1196734:1196735
@@ -262,8 +262,6 @@
 		// Connect thumbnail view
 		connect(mThumbnailView, SIGNAL(indexActivated(const QModelIndex&)),
 			mWindow, SLOT(slotThumbnailViewIndexActivated(const QModelIndex&)) );
-		connect(mThumbnailView->model(), SIGNAL(dataChanged(const QModelIndex&, const \
                QModelIndex&)),
-			mWindow, SLOT(slotDataChanged(const QModelIndex&, const QModelIndex&)) );
 		connect(mThumbnailView->selectionModel(), SIGNAL(selectionChanged(const \
QItemSelection&, const QItemSelection&)),  mWindow, SLOT(slotSelectionChanged()) );
 
@@ -437,8 +435,7 @@
 	void setupContextManager() {
 		KActionCollection* actionCollection = mWindow->actionCollection();
 
-		mContextManager = new ContextManager(mWindow);
-		mContextManager->setDirModel(mDirModel);
+		mContextManager = new ContextManager(mDirModel, mThumbnailView->selectionModel(), \
mWindow);  
 		// Create context manager items
 		FolderViewContextManagerItem* folderViewItem = new \
FolderViewContextManagerItem(mContextManager); @@ -716,28 +713,8 @@
 	}
 
 	void updateContextDependentComponents() {
-		// Gather info
-		KFileItemList selectedItemList;
-		KUrl url;
-		if (mCurrentPageId != StartPageId) {
-			QItemSelection selection = mThumbnailView->selectionModel()->selection();
-
-			Q_FOREACH(const QModelIndex& index, selection.indexes()) {
-				selectedItemList << mDirModel->itemForIndex(index);
-			}
-
-			// At least add current url if it's valid (it may not be in
-			// selectedItemList if we are viewing a non-browsable url, for example
-			// using http protocol)
-			url = currentUrl();
-			if (selectedItemList.isEmpty() && url.isValid()) {
-				KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
-				selectedItemList << item;
-			}
-		}
-
-		// Update
-		mContextManager->setContext(url, selectedItemList);
+		KUrl url = currentUrl();
+		mContextManager->setCurrentUrl(url);
 		mSaveBar->setCurrentUrl(url);
 		mSlideShow->setCurrentUrl(url);
 	}
@@ -1138,32 +1115,6 @@
 }
 
 
-void MainWindow::slotDataChanged(const QModelIndex& topLeft, const QModelIndex& \
                bottomRight) {
-	QModelIndexList selectionList = \
                d->mThumbnailView->selectionModel()->selectedIndexes();
-	if (selectionList.isEmpty()) {
-		return;
-	}
-
-	QModelIndexList changedList;
-	const QAbstractItemModel* model = topLeft.model();
-	for (int row=topLeft.row(); row <= bottomRight.row(); ++row) {
-		changedList << model->index(row, 0);
-	}
-
-	QModelIndexList& shortList = selectionList;
-	QModelIndexList& longList = changedList;
-	if (shortList.length() > longList.length()) {
-		qSwap(shortList, longList);
-	}
-	Q_FOREACH(const QModelIndex& index, shortList) {
-		if (longList.contains(index)) {
-			d->updateContextDependentComponents();
-			return;
-		}
-	}
-}
-
-
 void MainWindow::slotDirModelNewItems() {
 	if (d->mUrlToSelect.isValid()) {
 		d->selectUrlToSelect();
--- trunk/KDE/kdegraphics/gwenview/app/mainwindow.h #1196734:1196735
@@ -117,12 +117,6 @@
 
 	void slotSelectionChanged();
 
-	/**
-	 * Check if a selected index is among the changed indexes, if so, update
-	 * context
-	 */
-	void slotDataChanged(const QModelIndex&, const QModelIndex&);
-
 	void goToPrevious();
 	void goToNext();
 	void goToFirst();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic