File indexing completed on 2022-11-29 20:25:13

0001 /***************************************************************************
0002                    increasecounterplugin.cpp  -  description
0003                              -------------------
0004     begin                : Tue Jul 15 2008
0005     copyright            : (C) 2008 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 #include "increasecounterplugin.h"
0019 
0020 #include "ui_increasecounterpluginwidget.h"
0021 #include "pluginloader.h"
0022 
0023 #include <kiconloader.h>
0024 
0025 #include <QRegExp>
0026 
0027 IncreaseCounterPlugin::IncreaseCounterPlugin(PluginLoader *loader)
0028     : Plugin(loader), m_offset(0)
0029 {
0030     m_widget = new Ui::IncreaseCounterPluginWidget();
0031 }
0032 
0033 IncreaseCounterPlugin::~IncreaseCounterPlugin()
0034 {
0035     delete m_widget;
0036 }
0037 
0038 const QString IncreaseCounterPlugin::name() const
0039 {
0040     return i18n("Increase Counter");
0041 }
0042 
0043 const QPixmap IncreaseCounterPlugin::icon() const
0044 {
0045     return KIconLoader::global()->loadIcon("document-properties", KIconLoader::NoGroup, KIconLoader::SizeSmall);
0046 }
0047 
0048 QString IncreaseCounterPlugin::processFile(BatchRenamer *, int, const QString &filenameOrToken, EPluginType)
0049 {
0050     // Split string into prenum, number and postnum parts
0051     QRegExp splitit("(\\D*)(\\d+)(.*)");
0052 
0053     // Is there anything to increment ?
0054     if (splitit.exactMatch(filenameOrToken)) {
0055         QString prenum  = splitit.cap(1);
0056         long    tmp     = splitit.cap(2).toLong();
0057         QString postnum = splitit.cap(3);
0058 
0059         tmp += m_offset;
0060 
0061         QString tmpstr;
0062         return (prenum + tmpstr.sprintf("%0*li", splitit.cap(2).length(), tmp) + postnum);
0063     }
0064     return QString();
0065 }
0066 
0067 void IncreaseCounterPlugin::createUI(QWidget *parent) const
0068 {
0069     m_widget->setupUi(parent);
0070 
0071     connect(m_widget->spinOffset, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
0072             this, &IncreaseCounterPlugin::slotOffsetChanged);
0073 }
0074 
0075 void IncreaseCounterPlugin::slotOffsetChanged(int offset)
0076 {
0077     m_offset = offset;
0078     m_pluginLoader->sendUpdatePreview();
0079 }