File indexing completed on 2024-04-21 15:12:02

0001 /***************************************************************************
0002                           galleryhistory.cpp - combobox for gallery folder history
0003                           -------------------
0004     begin                : Tue Nov 13 2001
0005     copyright            : (C) 2001 by Klaas Freitag
0006     email                : freitag@suse.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *  This file may be distributed and/or modified under the terms of the    *
0012  *  GNU General Public License version 2 as published by the Free Software *
0013  *  Foundation and appearing in the file COPYING included in the           *
0014  *  packaging of this file.                                                *
0015  *
0016  *  As a special exception, permission is given to link this program       *
0017  *  with any version of the KADMOS ocr/icr engine of reRecognition GmbH,   *
0018  *  Kreuzlingen and distribute the resulting executable without            *
0019  *  including the source code for KADMOS in the source distribution.       *
0020  *
0021  *  As a special exception, permission is given to link this program       *
0022  *  with any edition of Qt, and distribute the resulting executable,       *
0023  *  without including the source code for Qt in the source distribution.   *
0024  *                                                                         *
0025  ***************************************************************************/
0026 
0027 #include "galleryhistory.h"
0028 
0029 #include <KLocalizedString>
0030 #include <kcombobox.h>
0031 
0032 #include "libfiletree/filetreeview.h"
0033 #include "libfiletree/filetreebranch.h"
0034 
0035 #include "kooka_logging.h"
0036 
0037 
0038 #define GALLERY_PATH_SEP    " - "
0039 
0040 GalleryHistory::GalleryHistory(QWidget *parent)
0041     : KComboBox(parent)
0042 {
0043     connect(this, QOverload<int>::of(&QComboBox::activated), this, &GalleryHistory::slotActivated);
0044     setEnabled(false);                  // no items added yet
0045 }
0046 
0047 // Data used for retrieving branch name and path - fixed format
0048 static QString entryData(const FileTreeBranch *branch, const QString &relPath)
0049 {
0050     return (branch->name() + GALLERY_PATH_SEP + relPath);
0051 }
0052 
0053 // Data for display - what the user sees
0054 static QString entryName(const FileTreeBranch *branch, const QString &relPath)
0055 {
0056     QString name;
0057 
0058     FileTreeView *view = static_cast<FileTreeView *>(branch->root()->treeWidget());
0059     if (view == nullptr) {
0060         return (relPath);    // get view that this belongs to
0061     }
0062 
0063     if (view->branches().count() > 1 || relPath == "/") { // multiple galleries, or
0064         // at the branch root
0065         name = branch->name();              // start with branch name
0066         if (relPath != "/") {
0067             name += GALLERY_PATH_SEP;    // add separator if path
0068         }
0069     }
0070 
0071     if (relPath != "/") {
0072         name += relPath;                // not root, add relative name
0073         if (name.endsWith("/")) {
0074             name.chop(1);    // without trailing slash
0075         }
0076     }
0077     return (name);
0078 }
0079 
0080 void GalleryHistory::slotPathRemoved(const FileTreeBranch *branch, const QString &relPath)
0081 {
0082     QString removeEntry = entryData(branch, relPath);
0083     qCDebug(KOOKA_LOG) << "removing" << removeEntry;
0084 
0085     int idx = findData(removeEntry);
0086     if (idx == -1) {
0087         return;    // not present, nothing to do
0088     }
0089 
0090     QString select = currentText();         // save current selection
0091     removeItem(idx);                    // remove that item
0092     setCurrentIndex(findText(select));          // restore current selection
0093     setEnabled(count() > 0);            // was the last removed?
0094 }
0095 
0096 void GalleryHistory::slotPathChanged(const FileTreeBranch *branch, const QString &relPath)
0097 {
0098     QString newData = entryData(branch, relPath);
0099     qCDebug(KOOKA_LOG) << "inserting" << newData;
0100 
0101     int idx = findData(newData);            // see if exists already
0102     if (idx != -1) {
0103         setCurrentIndex(idx);    // if so, just select that
0104     } else {                    // if not already present,
0105         // insert at top
0106         insertItem(0, entryName(branch, relPath), newData);
0107         setCurrentIndex(0);
0108     }
0109 
0110     setEnabled(true);                   // now have at least 1 item
0111 }
0112 
0113 void GalleryHistory::slotActivated(int idx)
0114 {
0115     QString branchName;
0116 
0117     QString relPath = itemData(idx).toString();
0118     int ix = relPath.indexOf(GALLERY_PATH_SEP);     // is the separator present?
0119     if (ix > 0) {                   // (multiple gallery roots)
0120         branchName = relPath.left(ix);          // split into root and path
0121         relPath = relPath.mid(ix + 3);
0122     }
0123 
0124     if (!relPath.endsWith("/")) {
0125         relPath = "/";    // it's the root
0126     }
0127     emit pathSelected(branchName, relPath);
0128 }