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

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2008 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "increasecounterplugin.h"
0005 
0006 #include "ui_increasecounterpluginwidget.h"
0007 #include "pluginloader.h"
0008 
0009 #include <QIcon>
0010 #include <QRegExp>
0011 
0012 IncreaseCounterPlugin::IncreaseCounterPlugin(PluginLoader *loader)
0013     : Plugin(loader), m_offset(0)
0014 {
0015     m_widget = new Ui::IncreaseCounterPluginWidget();
0016 }
0017 
0018 IncreaseCounterPlugin::~IncreaseCounterPlugin()
0019 {
0020     delete m_widget;
0021 }
0022 
0023 const QString IncreaseCounterPlugin::name() const
0024 {
0025     return i18n("Increase Counter");
0026 }
0027 
0028 const QIcon IncreaseCounterPlugin::icon() const
0029 {
0030     return QIcon::fromTheme("document-properties");
0031 }
0032 
0033 QString IncreaseCounterPlugin::processFile(BatchRenamer *, int, const QString &filenameOrToken, EPluginType)
0034 {
0035     // Split string into prenum, number and postnum parts
0036     QRegExp splitit("(\\D*)(\\d+)(.*)");
0037 
0038     // Is there anything to increment ?
0039     if (splitit.exactMatch(filenameOrToken)) {
0040         QString prenum  = splitit.cap(1);
0041         long    tmp     = splitit.cap(2).toLong();
0042         QString postnum = splitit.cap(3);
0043 
0044         tmp += m_offset;
0045 
0046         QString tmpstr;
0047         return (prenum + tmpstr.sprintf("%0*li", splitit.cap(2).length(), tmp) + postnum);
0048     }
0049     return QString();
0050 }
0051 
0052 void IncreaseCounterPlugin::createUI(QWidget *parent) const
0053 {
0054     m_widget->setupUi(parent);
0055 
0056     connect(m_widget->spinOffset, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
0057             this, &IncreaseCounterPlugin::slotOffsetChanged);
0058 }
0059 
0060 void IncreaseCounterPlugin::slotOffsetChanged(int offset)
0061 {
0062     m_offset = offset;
0063     m_pluginLoader->sendUpdatePreview();
0064 }
0065 
0066 #include "moc_increasecounterplugin.cpp"