From d4a27e6c647d49905976e07d339a9cf928145141 Mon Sep 17 00:00:00 2001
From: Helmut Schaa <helmut.schaa@googlemail.com>
Date: Thu, 17 May 2012 14:51:34 +0200
Subject: [PATCH] Add possibility to group selected images by basename

When shooting RAW + JPEG the resulting images most likely share the same
basename and only differ in the file extension. Add a context menu option
that allows to group images by basename.

This makes grouping of RAW files with their according JPEG files easy.

Signed-off-by: Helmut Schaa <helmut.schaa@googlemail.com>
---
 digikam/items/digikamimageview.cpp  |   26 ++++++++++++++++++++++++++
 digikam/items/digikamimageview.h    |    1 +
 digikam/utils/contextmenuhelper.cpp |    4 ++++
 digikam/utils/contextmenuhelper.h   |    1 +
 4 files changed, 32 insertions(+)

diff --git a/digikam/items/digikamimageview.cpp b/digikam/items/digikamimageview.cpp
index 692deeb..623bdfa 100644
--- a/digikam/items/digikamimageview.cpp
+++ b/digikam/items/digikamimageview.cpp
@@ -59,6 +59,7 @@
 #include "assignnameoverlay.h"
 #include "contextmenuhelper.h"
 #include "databaseaccess.h"
+#include "databaseinfocontainers.h"
 #include "ddragobjects.h"
 #include "digikamapp.h"
 #include "digikamimagedelegate.h"
@@ -406,6 +407,9 @@ void DigikamImageView::showContextMenuOnInfo(QContextMenuEvent* event, const Ima
     connect(&cmhelper, SIGNAL(signalCreateGroupByTime()),
             this, SLOT(createGroupByTimeFromSelection()));
 
+    connect(&cmhelper, SIGNAL(signalCreateGroupByBasename()),
+            this, SLOT(createGroupByBasenameFromSelection()));
+
     connect(&cmhelper, SIGNAL(signalUngroup()),
             this, SLOT(ungroupSelected()));
 
@@ -446,6 +450,9 @@ void DigikamImageView::showGroupContextMenu(const QModelIndex& index, QContextMe
     connect(&cmhelper, SIGNAL(signalCreateGroupByTime()),
             this, SLOT(createGroupByTimeFromSelection()));
 
+    connect(&cmhelper, SIGNAL(signalCreateGroupByBasename()),
+            this, SLOT(createGroupByBasenameFromSelection()));
+
     connect(&cmhelper, SIGNAL(signalUngroup()),
             this, SLOT(ungroupSelected()));
 
@@ -705,6 +712,25 @@ void DigikamImageView::createGroupByTimeFromSelection()
     }
 }
 
+void DigikamImageView::createGroupByBasenameFromSelection()
+{
+    QList<ImageInfo> selectedInfos = selectedImageInfosCurrentFirst();
+
+    while (selectedInfos.size() > 0)
+    {
+        QList<ImageInfo> group;
+        ImageInfo groupLeader = selectedInfos.takeFirst();
+        QString baseName = QFileInfo(groupLeader.imageCommonContainer().fileName).baseName();
+
+        while (selectedInfos.size() > 0 &&baseName == QFileInfo(selectedInfos.first().imageCommonContainer().fileName).baseName())
+        {
+            group.push_back(selectedInfos.takeFirst());
+        }
+
+        FileActionMngr::instance()->addToGroup(groupLeader, group);
+    }
+}
+
 void DigikamImageView::ungroupSelected()
 {
     FileActionMngr::instance()->ungroup(selectedImageInfos());
diff --git a/digikam/items/digikamimageview.h b/digikam/items/digikamimageview.h
index 62ce249..9b32289 100644
--- a/digikam/items/digikamimageview.h
+++ b/digikam/items/digikamimageview.h
@@ -82,6 +82,7 @@ public Q_SLOTS:
 
     void createGroupFromSelection();
     void createGroupByTimeFromSelection();
+    void createGroupByBasenameFromSelection();
     void ungroupSelected();
     void removeSelectedFromGroup();
 
diff --git a/digikam/utils/contextmenuhelper.cpp b/digikam/utils/contextmenuhelper.cpp
index 92b8c54..ab6705b 100644
--- a/digikam/utils/contextmenuhelper.cpp
+++ b/digikam/utils/contextmenuhelper.cpp
@@ -1011,6 +1011,10 @@ QList<QAction*> ContextMenuHelper::groupMenuActions(imageIds& ids)
         connect(closeActionDate, SIGNAL(triggered()), this, SIGNAL(signalCreateGroupByTime()));
         actions << closeActionDate;
 
+        QAction* closeActionBasename = new QAction(i18nc("@action:inmenu", "Group Selected By Basename"), this);
+        connect(closeActionBasename, SIGNAL(triggered()), this, SIGNAL(signalCreateGroupByBasename()));
+        actions << closeActionBasename;
+
         QAction* separator = new QAction(this);
         separator->setSeparator(true);
         actions << separator;
diff --git a/digikam/utils/contextmenuhelper.h b/digikam/utils/contextmenuhelper.h
index 3ef5b0a..75c9eba 100644
--- a/digikam/utils/contextmenuhelper.h
+++ b/digikam/utils/contextmenuhelper.h
@@ -104,6 +104,7 @@ Q_SIGNALS:
     void signalPopupTagsView();
     void signalCreateGroup();
     void signalCreateGroupByTime();
+    void signalCreateGroupByBasename();
     void signalUngroup();
     void signalRemoveFromGroup();
 
-- 
1.7.9.2

