File indexing completed on 2024-04-21 05:51:45

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #ifndef KRENAMEWINDOW_H
0005 #define KRENAMEWINDOW_H
0006 
0007 #include <kmainwindow.h>
0008 #include "batchrenamer.h"
0009 
0010 #include <QHash>
0011 #include <KTreeWidgetSearchLine>
0012 
0013 class KRenameModel;
0014 class KRenamePreviewModel;
0015 
0016 class QPushButton;
0017 class QAbstractItemView;
0018 class QComboBox;
0019 class QDialogButtonBox;
0020 class QLineEdit;
0021 class QModelIndex;
0022 class QStackedWidget;
0023 class QTabBar;
0024 class QTreeWidgetItem;
0025 
0026 class Plugin;
0027 class RichTextItemDelegate;
0028 
0029 namespace Ui
0030 {
0031 class KRenameFiles;
0032 class KRenameDestination;
0033 class KRenameSimple;
0034 class KRenamePlugins;
0035 class KRenameFilename;
0036 };
0037 
0038 /** KRenameWindow is the main window of KRename.
0039  *
0040  *  According to the window mode setting, a row of buttons
0041  *  is displayed at the bottom of the window.
0042  */
0043 class KRenameWindow : public KMainWindow
0044 {
0045     Q_OBJECT
0046 
0047     friend class KRenameImpl;
0048 
0049 public:
0050     explicit KRenameWindow(QWidget *parent = nullptr);
0051 
0052     ~KRenameWindow() override;
0053 
0054     /** The maximum number of pages in any gui mode.
0055      *  No gui mode can have more pages than this.
0056      */
0057     static const int MAX_PAGES = 4;
0058 
0059     /** This structure is used to describe a gui mode.
0060      *  It is required to map indexes to the right indexes
0061      *  of the widget stack and to retrieve the total number
0062      *  of pages per mode as well as the individual page titles.
0063      */
0064     typedef struct {
0065         const int   numPages;
0066         const char *pageTitles[KRenameWindow::MAX_PAGES];
0067         const int   mapIndex[KRenameWindow::MAX_PAGES];
0068         const char *pageIcons[KRenameWindow::MAX_PAGES];
0069     } TGuiMode;
0070 
0071     /**
0072      * Load UI configuration where necessary.
0073      */
0074     void loadConfig();
0075 
0076     /**
0077      * Save UI configuration where necessary.
0078      */
0079     void saveConfig();
0080 
0081     /** Set the count displayed on the files tab of KRename
0082      *  @param count typically the number of files in the list
0083      */
0084     void setCount(unsigned int count);
0085 
0086     /** Set the template for the filename.
0087      *
0088      *  @param templ the new template
0089      *  @param insert if true the template string will be inserted into the current
0090      *         template otherwise it will be replaced
0091      */
0092     void setFilenameTemplate(const QString &templ, bool insert = false);
0093 
0094     /** Set the template for the filename extension.
0095      *
0096      *  @param templ the new template
0097      *  @param insert if true the template string will be inserted into the current
0098      *         template otherwise it will be replaced
0099      */
0100     void setExtensionTemplate(const QString &templ, bool insert = false);
0101 
0102     /** Change the renmae mode
0103      *
0104      *  @param mode the renaming mode to use
0105      */
0106     void setRenameMode(ERenameMode mode);
0107 
0108     /** Set the destination url
0109      *
0110      * @param url destination url for eRenameMode_Copy,
0111      * eRenameMode_Move and eRenameMode_Link
0112      */
0113     void setDestinationUrl(const QUrl &url);
0114 
0115     /** Reset the display of the internal file list
0116      */
0117     void resetFileList();
0118 
0119     /** Set the KRename model
0120      *
0121      *  @param model a krename model
0122      */
0123     void setModel(KRenameModel *model);
0124 
0125     /** Set the preview model
0126      *
0127      * @param model a preview model
0128      */
0129     void setPreviewModel(KRenamePreviewModel *model);
0130 
0131     /**
0132      * @returns the destinationUrl selected by the user
0133      */
0134     const QUrl destinationUrl() const;
0135 
0136     /**
0137      * \returns a list of all file indexes that are selected
0138      *          in the filelist
0139      */
0140     QList<int> selectedFileItems() const;
0141 
0142     /**
0143      * \returns a list of all file indexes that are selected
0144      *          in the preview
0145      */
0146     QList<int> selectedFileItemsPreview() const;
0147 
0148     /**
0149      * \returns if file preview is enabled in the file tab
0150      */
0151     bool isPreviewEnabled() const;
0152 
0153     /**
0154      * \param bPreview enable/disable file preview in the file tab
0155      */
0156     void setPreviewEnabled(bool bPreview);
0157 
0158     /**
0159      * \returns if name display is enabled in the file tab
0160      */
0161     bool isPreviewNamesEnabled() const;
0162 
0163     /**
0164      * \param bPreview enable/disable file name display in the file tab
0165      */
0166     void setPreviewNamesEnabled(bool bPreview);
0167 
0168     /**
0169      * \returns the start index for numbers
0170      */
0171     int numberStartIndex() const;
0172 
0173     /**
0174      * \param index start index for numbers
0175      */
0176     void setNumberStartIndex(int index);
0177 
0178     /**
0179      * \returns the current sort mode in the file tab
0180      */
0181     int sortMode() const;
0182 
0183     /**
0184      * \param sortMode sets the current sort mode in the file tab
0185      * \param customToken custom sort token if sortMode == eSortMode_Token
0186      * \param customSortMode custom sort mode if sortMode == eSortMode_Token
0187      */
0188     void setSortMode(int sortMode, const QString &customToken, int customSortMode);
0189 
0190     /**
0191      * Specify the extension split mode.
0192      * \param splitMode split mode
0193      * \param dot dot t use for eSplitMode_CustomDot
0194      */
0195     void setExtensionSplitMode(ESplitMode splitMode, int dot);
0196 
0197     bool isAdvancedMode() const;
0198     void setAdvancedMode(bool bAdvanced);
0199 
0200     /**
0201      * Shows the filename tab
0202      * This might be useful, if the user
0203      * passed filenames via the commandline
0204      * and wants to immediately work with the
0205      * filename template.
0206      */
0207     void showFilenameTab();
0208 
0209 public Q_SLOTS:
0210     /** Resets the enabled/disabled state of all GUI elements correctly
0211      */
0212     void slotEnableControls();
0213 
0214 Q_SIGNALS:
0215     /** Called when the krenamewindow
0216      *  needs an update of the file cound
0217      *
0218      *  \see setCount
0219      */
0220     void updateCount();
0221 
0222     /** Called whenever the user changes the rename mode
0223      *
0224      *  @param mode the renaming mode selected by the user
0225      */
0226     void renameModeChanged(ERenameMode mode);
0227 
0228     /** Called whenever the user changes the option
0229      *  if existing files maybe overwritten
0230      *
0231      *  @param bool overwrite if true existing files maybe overwritten during renaming
0232      */
0233     void overwriteFilesChanged(bool overwrite);
0234 
0235     /** Called whenever the user has changed the template for the filename
0236      *
0237      *  @param filename the new template for the filename
0238      */
0239     void filenameTemplateChanged(const QString &filename);
0240 
0241     /** Called whenever the user has changed the template for the extension
0242      *
0243      *  @param filename the new template for the extension
0244      */
0245     void extensionTemplateChanged(const QString &extension);
0246 
0247     /** Called whenever the user change the splitmode between filename and extension
0248      *
0249      *  @param splitMode the mode which is used to split filename and extension
0250      *  @param dot the n-th dot to use for splitting if splitMode = eSplitMode_CustomDot
0251      */
0252     void extensionSplitModeChanged(ESplitMode splitMode, int dot);
0253 
0254     /** Called whenever the user changes the start index
0255      *
0256      *  @param index the new start index for numberings
0257      */
0258     void startIndexChanged(int index);
0259 
0260     /** Called whenever the user changes the file preview state
0261      *
0262      *  @param enable if true display previews
0263      *  @param filenames if true display filenames next to preview (if enable is false
0264      *                   this parameter has to be ignored.
0265      */
0266     void filePreviewChanged(bool enable, bool filenames);
0267 
0268     /** Called whenever the GUI wants an updated preview
0269      */
0270     void updatePreview();
0271 
0272     /** Called when the user requests the advanced numbering dialog
0273      */
0274     void showAdvancedNumberingDialog();
0275 
0276     /** Called when the user requests the insert part filename dialog
0277      */
0278     void showInsertPartFilenameDialog();
0279 
0280     /** Called when the user requests the find and repalce dialog
0281      */
0282     void showFindReplaceDialog();
0283 
0284     /** Called when the user wants to add more files
0285      */
0286     void addFiles();
0287 
0288     /** Called when the user wants to remove all files
0289      */
0290     void removeAllFiles();
0291 
0292     /** Called when the user wants to remove all selected files
0293      *
0294      *  \see selectedFileItems() to retrieve the selected files
0295      */
0296     void removeFiles();
0297 
0298     /** Show a token help dialog which inserts a token
0299      *  into a KLineEdit
0300      *
0301      *  \param edit the QLineEdit which is used to insert tokens selected by the user
0302      */
0303     void showTokenHelpDialog(QLineEdit *edit);
0304 
0305     /** Called when the user wants to rename with current settings
0306      */
0307     void accepted();
0308 
0309 private Q_SLOTS:
0310     void slotBack();
0311     void slotNext();
0312     void slotFinish();
0313 
0314     /** Changes the current page to the page with the given index
0315      *
0316      *  @param index the index of the page to show
0317      */
0318     void showPage(int index);
0319 
0320     /** Called when one of the checkboxes for
0321      *  rename, copy, more or link was clicked
0322      *  to change the rename mode.
0323      */
0324     void slotRenameModeChanged();
0325 
0326     /** Called whenever one of the template fields has changed
0327      */
0328     void slotTemplateChanged();
0329 
0330     /** Called whenever one of the template fields in wizard mode has changed
0331      */
0332     void slotSimpleTemplateChanged();
0333 
0334     /** Emits the showTokenHelpDialog signal with the appropriate
0335      *  KLineEdit
0336      */
0337     void slotTokenHelpRequested();
0338 
0339     /** Emits the showTokenHelpDialog signal with the appropriate
0340      *  KLineEdit
0341      */
0342     void slotTokenHelpRequestedWizard1();
0343 
0344     /** Emits the showTokenHelpDialog signal with the appropriate
0345      *  KLineEdit
0346      */
0347     void slotTokenHelpRequestedWizard2();
0348 
0349     /** Emits the showTokenHelpDialog signal with the appropriate
0350      *  KLineEdit
0351      */
0352     void slotTokenHelpRequestedWizard3();
0353 
0354     /** Emits the showTokenHelpDialog signal with the appropriate
0355      *  KLineEdit
0356      */
0357     void slotTokenHelpRequestedWizard4();
0358 
0359     /** Called when the user selects another extension split mode
0360      */
0361     void slotExtensionSplitModeChanged(int index);
0362 
0363     /** Called when one of the preview checkboxes is clicked.
0364      */
0365     void slotPreviewChanged();
0366 
0367     /** Called whenever the user changes the current sort mode
0368      *
0369      *  @param index currently selected sort index
0370      */
0371     void slotSortChanged(int index);
0372 
0373     /** Called whenever the possible maximum value of
0374      *  dots in a filename has changed.
0375      *
0376      *  @param dots the maximum number of dots in a filename
0377      *              that can be used to separate fileanem and extension
0378      */
0379     void slotMaxDotsChanged(int dots);
0380 
0381     /** Called whenever the user clicks a file
0382      *  in a listview to open it.
0383      *
0384      *  @param index the model index of the requested file in a model
0385      */
0386     void slotOpenFile(const QModelIndex &index);
0387 
0388     /** Called when the user wants to move files up
0389      *  in the file list box.
0390      */
0391     void slotMoveUp();
0392 
0393     /** Called when the user wants to move files down
0394      *  in the file list box.
0395      */
0396     void slotMoveDown();
0397 
0398     /** Called when the user wants to move files up
0399      *  in the preview.
0400      */
0401     void slotMoveUpPreview();
0402 
0403     /** Called when the user wants to move files down
0404      *  in the preview.
0405      */
0406     void slotMoveDownPreview();
0407 
0408     /** Called when the user selects a plugin in the plugins tab
0409      */
0410     void slotPluginChanged(QTreeWidgetItem *selected);
0411 
0412     /** Called when a plugin is enabled or disabled
0413      */
0414     void slotPluginEnabled();
0415 
0416     /** Called when the user changes the start index
0417      *  in the gui
0418      */
0419     void slotSimpleStartIndexChanged();
0420 
0421     /**
0422      * Called when the user clicked the enter templates
0423      * link in StartUpInfo.
0424      */
0425     void slotGotoTemplatesPage();
0426 
0427 private:
0428     /** Configures the GUI for the current GUI mode
0429      */
0430     void setupGui();
0431 
0432     /** Setup all signals and slots
0433      */
0434     void setupSlots();
0435 
0436     /** Load all plugins
0437      */
0438     void setupPlugins();
0439 
0440     /** Set standard KDE icons on UI elements
0441      *  as this cannot be done from within designer
0442      */
0443     void setupIcons();
0444 
0445     /** Get a template for the prefix or suffix in wizard mode
0446      *  from 2 combo boxes
0447      *
0448      *  @param combo the combobox used to select a default prefix/suffix (e.g. date)
0449      *  @param comboCustom additional user defined text
0450      *  @returns a template string
0451      */
0452     QString getPrefixSuffixSimple(QComboBox *combo, QComboBox *comboCustom);
0453 
0454     /** Get a template for the filename or extension in wizard mode
0455      *  from 2 combo boxes
0456      *
0457      *  @param combo the combobox used to select a default filename (e.g. lowercase)
0458      *  @param comboCustom additional user defined text
0459      *  @returns a template string
0460      */
0461     QString getFilenameSimple(QComboBox *combo, QComboBox *comboCustom);
0462 
0463     /** Set the GUI elements in the simple filename tab using a template
0464      *  that has specified in advanced mode
0465      *
0466      *  @param filename template of the filename
0467      *  @param extension template of the extension
0468      */
0469     void setSimpleTemplate(const QString &filename, const QString &extension);
0470 
0471     /** Emit signals that the template has been changed in some way by the user
0472      *
0473      *  @param filename template of the filename
0474      *  @param extension template of the extension
0475      */
0476     void templatesChanged(const QString &filename, const QString &extension);
0477 
0478     void setPrefixSuffixSimple(QComboBox *combo, QComboBox *comboCustom, const QString &templ);
0479 
0480     void moveUp(const QList<int> &selected, QAbstractItemView *view);
0481     void moveDown(const QList<int> &selected, QAbstractItemView *view);
0482 
0483     /**
0484      * \param index (0 or 1) index of the column
0485      * \returns the column width of column index
0486      */
0487     int previewColumnWidth(int index);
0488 
0489     /**
0490      * Set the width of the specified column
0491      * \param index (0 or 1) index of the column
0492      * \param width width in pixels
0493      */
0494     void setPreviewColumnWidth(int index, int width);
0495 
0496     void blockSignalsRecursive(QObject *obj, bool b);
0497 
0498 private:
0499     int               m_curPage;   /// The index of the current page in the current gui mode
0500     const TGuiMode   *m_guiMode;   /// The description structure of the current gui mode
0501     int               m_fileCount; /// Current number of files; used for enabled disabled state
0502 
0503     QStackedWidget   *m_stack;
0504     QDialogButtonBox *m_buttons;
0505 
0506     QTabBar          *m_tabBar;    /// The tabbar to switch pages in advanced mode
0507 
0508     QPushButton      *m_buttonClose;
0509     QPushButton      *m_buttonFinish;
0510 
0511     Ui::KRenameFiles       *m_pageFiles;
0512     Ui::KRenameDestination *m_pageDests;
0513     //Ui::KRenameSimple*      m_pageSimple;
0514     Ui::KRenamePlugins     *m_pagePlugins;
0515     Ui::KRenameFilename    *m_pageFilename;
0516 
0517     QHash<QString, QWidget *> m_pluginsWidgetHash;
0518     QHash<QString, Plugin *>  m_pluginsHash;
0519 
0520     RichTextItemDelegate   *m_delegate;
0521 };
0522 
0523 #endif // KRENAMEWINDOW_H