File indexing completed on 2022-10-04 17:30:14

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "insertpartfilenamedlg.h"
0005 
0006 #include <QDialogButtonBox>
0007 #include <QVBoxLayout>
0008 
0009 InsertPartFilenameDlg::InsertPartFilenameDlg(const QString &filename, QWidget *parent)
0010     : QDialog(parent), m_start(-1), m_end(0)
0011 {
0012     QVBoxLayout *layout = new QVBoxLayout(this);
0013     QWidget     *widget = new QWidget(this);
0014 
0015     m_widget.setupUi(widget);
0016     m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal, this);
0017 
0018     m_widget.textFilename->setText(filename);
0019     m_widget.textFilename->setCursorPosition(0);
0020 
0021     layout->addWidget(widget);
0022     layout->addWidget(m_buttons);
0023 
0024     connect(m_buttons, &QDialogButtonBox::accepted,
0025             this, &InsertPartFilenameDlg::accept);
0026     connect(m_buttons, &QDialogButtonBox::rejected,
0027             this, &InsertPartFilenameDlg::reject);
0028 
0029     connect(m_widget.checkInvert, &QCheckBox::clicked,
0030             this, &InsertPartFilenameDlg::slotUpdateKRenameCommand);
0031     connect(m_widget.textFilename, &SelectionSafeLineEdit::selectionChanged,
0032             this, &InsertPartFilenameDlg::slotSelectionChanged);
0033     connect(m_widget.comboConvert, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
0034             this, &InsertPartFilenameDlg::slotSelectionChanged);
0035 
0036     this->slotUpdateKRenameCommand();
0037 }
0038 
0039 void InsertPartFilenameDlg::slotSelectionChanged()
0040 {
0041     m_start      = m_widget.textFilename->selectionStart();
0042     QString text = m_widget.textFilename->selectedText();
0043     m_end        = m_start + text.length();
0044 
0045     slotUpdateKRenameCommand();
0046 }
0047 
0048 void InsertPartFilenameDlg::slotUpdateKRenameCommand()
0049 {
0050     m_command        = "";
0051     bool hasSelected = m_start != -1;
0052     int  cursorPos   = m_widget.textFilename->cursorPosition();
0053     int  end         = m_end;
0054     int  start       = m_start;
0055 
0056     char conversionflag = '$';
0057     if (m_widget.comboConvert->currentIndex() == 1) {
0058         conversionflag = '%';
0059     } else if (m_widget.comboConvert->currentIndex() == 2) {
0060         conversionflag = '&';
0061     } else if (m_widget.comboConvert->currentIndex() == 3) {
0062         conversionflag = '*';
0063     }
0064 
0065     if (!m_widget.textFilename->text().isEmpty()) {
0066         if (m_widget.checkInvert->isChecked() && hasSelected) {
0067             // inverted
0068             if (end) {
0069                 start++;
0070                 end++;
0071                 if (start > 1) {
0072                     m_command = QString("[%1;%2]").arg(conversionflag).arg(start - 1);
0073                 }
0074 
0075                 if (end <= (signed int)m_widget.textFilename->text().length()) {
0076                     m_command.append(QString("[%1%2-[length]]").arg(conversionflag).arg(end));
0077                 }
0078             }
0079         } else if (m_widget.checkInvert->isChecked() && !hasSelected) {
0080             m_command = QString("[%1").arg(conversionflag) + QString("1;%1][%3%2-[length]]").arg(cursorPos).arg(cursorPos + 1);
0081         } else if (!m_widget.checkInvert->isChecked() && hasSelected) {
0082             if (end) {
0083                 start++;
0084                 end++;
0085                 if (end <= (signed int)m_widget.textFilename->text().length()) {
0086                     m_command = QString("[%1%2;%3]").arg(conversionflag).arg(start).arg(end - start);
0087                 } else {
0088                     m_command = QString("[%1%2-[length]]").arg(conversionflag).arg(start);
0089                 }
0090             }
0091         } else if (!m_widget.checkInvert->isChecked() && !hasSelected) {
0092             m_command = QString("[%1%2-[length]]").arg(conversionflag).arg(cursorPos);
0093         }
0094 
0095     }
0096 
0097     m_widget.labelPreview->setText(m_command);
0098 }