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

0001 /***************************************************************************
0002                insertpartfilenamedlg.cpp  -  description
0003                              -------------------
0004     begin                : Sat Jun 30 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 #include "insertpartfilenamedlg.h"
0019 
0020 #include <QDialogButtonBox>
0021 #include <QVBoxLayout>
0022 
0023 InsertPartFilenameDlg::InsertPartFilenameDlg(const QString &filename, QWidget *parent)
0024     : QDialog(parent), m_start(-1), m_end(0)
0025 {
0026     QVBoxLayout *layout = new QVBoxLayout(this);
0027     QWidget     *widget = new QWidget(this);
0028 
0029     m_widget.setupUi(widget);
0030     m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal, this);
0031 
0032     m_widget.textFilename->setText(filename);
0033     m_widget.textFilename->setCursorPosition(0);
0034 
0035     layout->addWidget(widget);
0036     layout->addWidget(m_buttons);
0037 
0038     connect(m_buttons, &QDialogButtonBox::accepted,
0039             this, &InsertPartFilenameDlg::accept);
0040     connect(m_buttons, &QDialogButtonBox::rejected,
0041             this, &InsertPartFilenameDlg::reject);
0042 
0043     connect(m_widget.checkInvert, &QCheckBox::clicked,
0044             this, &InsertPartFilenameDlg::slotUpdateKRenameCommand);
0045     connect(m_widget.textFilename, &SelectionSafeLineEdit::selectionChanged,
0046             this, &InsertPartFilenameDlg::slotSelectionChanged);
0047     connect(m_widget.comboConvert, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
0048             this, &InsertPartFilenameDlg::slotSelectionChanged);
0049 
0050     this->slotUpdateKRenameCommand();
0051 }
0052 
0053 void InsertPartFilenameDlg::slotSelectionChanged()
0054 {
0055     m_start      = m_widget.textFilename->selectionStart();
0056     QString text = m_widget.textFilename->selectedText();
0057     m_end        = m_start + text.length();
0058 
0059     slotUpdateKRenameCommand();
0060 }
0061 
0062 void InsertPartFilenameDlg::slotUpdateKRenameCommand()
0063 {
0064     m_command        = "";
0065     bool hasSelected = m_start != -1;
0066     int  cursorPos   = m_widget.textFilename->cursorPosition();
0067     int  end         = m_end;
0068     int  start       = m_start;
0069 
0070     char conversionflag = '$';
0071     if (m_widget.comboConvert->currentIndex() == 1) {
0072         conversionflag = '%';
0073     } else if (m_widget.comboConvert->currentIndex() == 2) {
0074         conversionflag = '&';
0075     } else if (m_widget.comboConvert->currentIndex() == 3) {
0076         conversionflag = '*';
0077     }
0078 
0079     if (!m_widget.textFilename->text().isEmpty()) {
0080         if (m_widget.checkInvert->isChecked() && hasSelected) {
0081             // inverted
0082             if (end) {
0083                 start++;
0084                 end++;
0085                 if (start > 1) {
0086                     m_command = QString("[%1;%2]").arg(conversionflag).arg(start - 1);
0087                 }
0088 
0089                 if (end <= (signed int)m_widget.textFilename->text().length()) {
0090                     m_command.append(QString("[%1%2-[length]]").arg(conversionflag).arg(end));
0091                 }
0092             }
0093         } else if (m_widget.checkInvert->isChecked() && !hasSelected) {
0094             m_command = QString("[%1").arg(conversionflag) + QString("1;%1][%3%2-[length]]").arg(cursorPos).arg(cursorPos + 1);
0095         } else if (!m_widget.checkInvert->isChecked() && hasSelected) {
0096             if (end) {
0097                 start++;
0098                 end++;
0099                 if (end <= (signed int)m_widget.textFilename->text().length()) {
0100                     m_command = QString("[%1%2;%3]").arg(conversionflag).arg(start).arg(end - start);
0101                 } else {
0102                     m_command = QString("[%1%2-[length]]").arg(conversionflag).arg(start);
0103                 }
0104             }
0105         } else if (!m_widget.checkInvert->isChecked() && !hasSelected) {
0106             m_command = QString("[%1%2-[length]]").arg(conversionflag).arg(cursorPos);
0107         }
0108 
0109     }
0110 
0111     m_widget.labelPreview->setText(m_command);
0112 }