File indexing completed on 2022-11-23 12:36:18

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