renaming utility: Improve coding style

This commit is contained in:
Martchus 2017-01-06 22:15:39 +01:00
parent 5472c560f3
commit d5c2dd5f5b
4 changed files with 110 additions and 137 deletions

View File

@ -137,71 +137,73 @@ void RenameFilesDialog::showScriptFileSelectionDlg()
void RenameFilesDialog::startGeneratingPreview() void RenameFilesDialog::startGeneratingPreview()
{ {
if(!m_engine->isBusy()) { if(m_engine->isBusy()) {
QDir selectedDir(directory()); return;
m_ui->notificationLabel->setHidden(false); }
if(selectedDir.exists()) { QDir selectedDir(directory());
QString program; m_ui->notificationLabel->setHidden(false);
if(m_ui->sourceFileStackedWidget->currentIndex() == 0) { if(selectedDir.exists()) {
program = m_ui->javaScriptPlainTextEdit->toPlainText(); QString program;
if(m_ui->sourceFileStackedWidget->currentIndex() == 0) {
program = m_ui->javaScriptPlainTextEdit->toPlainText();
} else {
QString fileName = m_ui->scriptFilePathLineEdit->text();
if(fileName.isEmpty()) {
m_ui->notificationLabel->setText(tr("There is no external script file is selected."));
} else { } else {
QString fileName = m_ui->scriptFilePathLineEdit->text(); QFile file(fileName);
if(fileName.isEmpty()) { if(file.open(QFile::ReadOnly)) {
m_ui->notificationLabel->setText(tr("There is no external script file is selected.")); QTextStream textStream(&file);
program = textStream.readAll();
} else { } else {
QFile file(fileName); m_ui->notificationLabel->setText(tr("Unable to open external script file."));
if(file.open(QFile::ReadOnly)) {
QTextStream textStream(&file);
program = textStream.readAll();
} else {
m_ui->notificationLabel->setText(tr("Unable to open external script file."));
}
} }
} }
if(!program.isEmpty()) { }
if(m_engine->setProgram(program)) { if(!program.isEmpty()) {
m_ui->notificationLabel->setText(tr("Generating preview ...")); if(m_engine->setProgram(program)) {
m_ui->notificationLabel->setNotificationType(NotificationType::Progress); m_ui->notificationLabel->setText(tr("Generating preview ..."));
m_ui->abortClosePushButton->setText(tr("Abort")); m_ui->notificationLabel->setNotificationType(NotificationType::Progress);
m_ui->generatePreviewPushButton->setHidden(true); m_ui->abortClosePushButton->setText(tr("Abort"));
m_ui->applyChangingsPushButton->setHidden(true); m_ui->generatePreviewPushButton->setHidden(true);
m_engine->generatePreview(directory(), m_ui->includeSubdirsCheckBox->isChecked()); m_ui->applyChangingsPushButton->setHidden(true);
} else { m_engine->generatePreview(directory(), m_ui->includeSubdirsCheckBox->isChecked());
m_engine->clearPreview();
if(m_engine->errorLineNumber()) {
m_ui->notificationLabel->setText(tr("The script is not valid.\nError in line %1: %3")
.arg(m_engine->errorLineNumber()).arg(m_engine->errorMessage()));
} else {
m_ui->notificationLabel->setText(tr("An error occured when parsing the script: %1").arg(m_engine->errorMessage()));
}
m_ui->notificationLabel->setNotificationType(NotificationType::Warning);
}
} else { } else {
m_engine->clearPreview(); m_engine->clearPreview();
m_ui->notificationLabel->setNotificationType(NotificationType::Warning); if(m_engine->errorLineNumber()) {
if(m_ui->notificationLabel->text().isEmpty()) { m_ui->notificationLabel->setText(tr("The script is not valid.\nError in line %1: %3")
m_ui->notificationLabel->setText(tr("The script is empty.")); .arg(m_engine->errorLineNumber()).arg(m_engine->errorMessage()));
} else {
m_ui->notificationLabel->setText(tr("An error occured when parsing the script: %1").arg(m_engine->errorMessage()));
} }
m_ui->notificationLabel->setNotificationType(NotificationType::Warning);
} }
} else { } else {
m_engine->clearPreview(); m_engine->clearPreview();
m_ui->notificationLabel->setText(tr("The selected directory doesn't exist."));
m_ui->notificationLabel->setNotificationType(NotificationType::Warning); m_ui->notificationLabel->setNotificationType(NotificationType::Warning);
if(m_ui->notificationLabel->text().isEmpty()) {
m_ui->notificationLabel->setText(tr("The script is empty."));
}
} }
} else {
m_engine->clearPreview();
m_ui->notificationLabel->setText(tr("The selected directory doesn't exist."));
m_ui->notificationLabel->setNotificationType(NotificationType::Warning);
} }
} }
void RenameFilesDialog::startApplyChangings() void RenameFilesDialog::startApplyChangings()
{ {
if(!m_engine->isBusy()) { if(m_engine->isBusy()) {
m_ui->notificationLabel->setText(tr("Applying changings ...")); return;
m_ui->notificationLabel->setNotificationType(NotificationType::Progress);
m_ui->abortClosePushButton->setText(tr("Abort"));
m_ui->generatePreviewPushButton->setHidden(true);
m_ui->applyChangingsPushButton->setHidden(true);
m_engine->applyChangings();
} }
m_ui->notificationLabel->setText(tr("Applying changings ..."));
m_ui->notificationLabel->setNotificationType(NotificationType::Progress);
m_ui->abortClosePushButton->setText(tr("Abort"));
m_ui->generatePreviewPushButton->setHidden(true);
m_ui->applyChangingsPushButton->setHidden(true);
m_engine->applyChangings();
} }
void RenameFilesDialog::showPreviewProgress(int itemsProcessed, int errorsOccured) void RenameFilesDialog::showPreviewProgress(int itemsProcessed, int errorsOccured)
@ -261,54 +263,58 @@ void RenameFilesDialog::showChangsingsResults()
void RenameFilesDialog::currentItemSelected(const QItemSelection &, const QItemSelection &) void RenameFilesDialog::currentItemSelected(const QItemSelection &, const QItemSelection &)
{ {
if(!m_changingSelection) { if(m_changingSelection) {
m_changingSelection = true; return;
m_ui->previewTreeView->selectionModel()->clear();
for(const QModelIndex &row : m_ui->currentTreeView->selectionModel()->selectedRows()) {
QModelIndex currentIndex = m_engine->currentModel()->mapToSource(row);
QModelIndex counterpartIndex = m_engine->model()->counterpart(
currentIndex, 1);
if(!counterpartIndex.isValid()) {
counterpartIndex = currentIndex;
}
QModelIndex previewIndex = m_engine->previewModel()->mapFromSource(counterpartIndex);
if(previewIndex.isValid()) {
QModelIndex parent = previewIndex.parent();
if(parent.isValid()) {
m_ui->previewTreeView->expand(m_engine->previewModel()->index(parent.row(), parent.column(), parent.parent()));
}
m_ui->previewTreeView->scrollTo(previewIndex);
m_ui->previewTreeView->selectionModel()->select(previewIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select);
}
}
m_changingSelection = false;
} }
m_changingSelection = true;
m_ui->previewTreeView->selectionModel()->clear();
for(const QModelIndex &row : m_ui->currentTreeView->selectionModel()->selectedRows()) {
QModelIndex currentIndex = m_engine->currentModel()->mapToSource(row);
QModelIndex counterpartIndex = m_engine->model()->counterpart(
currentIndex, 1);
if(!counterpartIndex.isValid()) {
counterpartIndex = currentIndex;
}
QModelIndex previewIndex = m_engine->previewModel()->mapFromSource(counterpartIndex);
if(previewIndex.isValid()) {
QModelIndex parent = previewIndex.parent();
if(parent.isValid()) {
m_ui->previewTreeView->expand(m_engine->previewModel()->index(parent.row(), parent.column(), parent.parent()));
}
m_ui->previewTreeView->scrollTo(previewIndex);
m_ui->previewTreeView->selectionModel()->select(previewIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select);
}
}
m_changingSelection = false;
} }
void RenameFilesDialog::previewItemSelected(const QItemSelection &, const QItemSelection &) void RenameFilesDialog::previewItemSelected(const QItemSelection &, const QItemSelection &)
{ {
if(!m_changingSelection) { if(m_changingSelection) {
m_changingSelection = true; return;
m_ui->currentTreeView->selectionModel()->clear();
for(const QModelIndex &row : m_ui->previewTreeView->selectionModel()->selectedRows()) {
QModelIndex previewIndex = m_engine->previewModel()->mapToSource(row);
QModelIndex counterpartIndex = m_engine->model()->counterpart(
previewIndex, 0);
if(!counterpartIndex.isValid()) {
counterpartIndex = previewIndex;
}
QModelIndex currentIndex = m_engine->currentModel()->mapFromSource(counterpartIndex);
if(currentIndex.isValid()) {
QModelIndex parent = currentIndex.parent();
if(parent.isValid()) {
m_ui->currentTreeView->expand(m_engine->currentModel()->index(parent.row(), parent.column(), parent.parent()));
}
m_ui->currentTreeView->scrollTo(currentIndex);
m_ui->currentTreeView->selectionModel()->select(currentIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select);
}
}
m_changingSelection = false;
} }
m_changingSelection = true;
m_ui->currentTreeView->selectionModel()->clear();
for(const QModelIndex &row : m_ui->previewTreeView->selectionModel()->selectedRows()) {
QModelIndex previewIndex = m_engine->previewModel()->mapToSource(row);
QModelIndex counterpartIndex = m_engine->model()->counterpart(
previewIndex, 0);
if(!counterpartIndex.isValid()) {
counterpartIndex = previewIndex;
}
QModelIndex currentIndex = m_engine->currentModel()->mapFromSource(counterpartIndex);
if(currentIndex.isValid()) {
QModelIndex parent = currentIndex.parent();
if(parent.isValid()) {
m_ui->currentTreeView->expand(m_engine->currentModel()->index(parent.row(), parent.column(), parent.parent()));
}
m_ui->currentTreeView->scrollTo(currentIndex);
m_ui->currentTreeView->selectionModel()->select(currentIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select);
}
}
m_changingSelection = false;
} }
void RenameFilesDialog::pasteScriptFromFile(const QString &fileName) void RenameFilesDialog::pasteScriptFromFile(const QString &fileName)
@ -341,12 +347,13 @@ void RenameFilesDialog::abortClose()
void RenameFilesDialog::pasteScriptFromClipboard() void RenameFilesDialog::pasteScriptFromClipboard()
{ {
QString script = QApplication::clipboard()->text(); const QString script = QApplication::clipboard()->text();
if(!script.isEmpty()) { if(script.isEmpty()) {
m_ui->javaScriptPlainTextEdit->setPlainText(script);
} else {
QMessageBox::warning(this, windowTitle(), tr("Clipboard contains no text.")); QMessageBox::warning(this, windowTitle(), tr("Clipboard contains no text."));
return;
} }
m_ui->javaScriptPlainTextEdit->setPlainText(script);
} }
void RenameFilesDialog::pasteDefaultExampleScript() void RenameFilesDialog::pasteDefaultExampleScript()
@ -356,10 +363,10 @@ void RenameFilesDialog::pasteDefaultExampleScript()
void RenameFilesDialog::showTreeViewContextMenu() void RenameFilesDialog::showTreeViewContextMenu()
{ {
if(QObject *sender = QObject::sender()) { if(const QTreeView *sender = qobject_cast<const QTreeView *>(QObject::sender())) {
QMenu menu; QMenu menu;
menu.addAction(tr("Expand all"), sender, SLOT(expandAll())); connect(menu.addAction(tr("Expand all")), &QAction::trigger, sender, &QTreeView::expandAll);
menu.addAction(tr("Collapse all"), sender, SLOT(collapseAll())); connect(menu.addAction(tr("Collapse all")), &QAction::trigger, sender, &QTreeView::collapseAll);
menu.exec(QCursor::pos()); menu.exec(QCursor::pos());
} }
} }

View File

@ -2,7 +2,6 @@
#include <QDir> #include <QDir>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug>
namespace RenamingUtility { namespace RenamingUtility {
@ -65,8 +64,6 @@ const QString &FileSystemItem::currentName() const
: emptyStr(); : emptyStr();
case ItemStatus::Current: case ItemStatus::Current:
return m_name; return m_name;
default:
return emptyStr();
} }
} }
@ -97,8 +94,6 @@ const QString &FileSystemItem::newName() const
: emptyStr(); : emptyStr();
case ItemStatus::New: case ItemStatus::New:
return m_name; return m_name;
default:
return emptyStr();
} }
} }
@ -122,8 +117,6 @@ bool FileSystemItem::setNewName(const QString &newName)
case ItemStatus::New: case ItemStatus::New:
setName(newName); setName(newName);
return true; return true;
default: // avoid warning
return false;
} }
} }
@ -206,7 +199,7 @@ bool FileSystemItem::hasSibling(const QString &name) const
{ {
if(m_parent) { if(m_parent) {
const QList<FileSystemItem *> &siblings = m_parent->children(); const QList<FileSystemItem *> &siblings = m_parent->children();
for(FileSystemItem *siblingItem : siblings) { for(const FileSystemItem *siblingItem : siblings) {
if(siblingItem == this) { if(siblingItem == this) {
continue; continue;
} }

View File

@ -94,16 +94,6 @@ QVariant FileSystemItemModel::data(const QModelIndex &index, int role) const
return QBrush(Qt::gray); return QBrush(Qt::gray);
} }
break; break;
/*case Qt::CheckStateRole:
if(item->checkable() && index.column() == 1) {
return item->checked() ? Qt::Checked : Qt::Unchecked;
}
break;
case CheckableRole:
if(index.column() == 1) {
return item->checkable();
}
break;*/
case ErrorStatusRole: case ErrorStatusRole:
return item->errorOccured(); return item->errorOccured();
default: default:
@ -139,10 +129,6 @@ bool FileSystemItemModel::setData(const QModelIndex &index, const QVariant &valu
default: ; default: ;
} }
break; break;
/*case Qt::CheckStateRole:
item->setChecked(value.toInt() == Qt::Checked);
emit dataChanged(index, index, QVector<int>() << role);
return true;*/
case ErrorStatusRole: case ErrorStatusRole:
item->setErrorOccured(value.toBool()); item->setErrorOccured(value.toBool());
emit dataChanged(index, index, QVector<int>() << role << Qt::DecorationRole); emit dataChanged(index, index, QVector<int>() << role << Qt::DecorationRole);
@ -155,18 +141,6 @@ bool FileSystemItemModel::setData(const QModelIndex &index, const QVariant &valu
return false; return false;
} }
Qt::ItemFlags FileSystemItemModel::flags(const QModelIndex &index) const
{
/*if(index.isValid() && index.column() == 1) {
if(FileSystemItem *item = reinterpret_cast<FileSystemItem *>(index.internalPointer())) {
if(item->checkable()) {
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
}
}
}*/
return QAbstractItemModel::flags(index);
}
QVariant FileSystemItemModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant FileSystemItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
switch(orientation) { switch(orientation) {
@ -253,10 +227,10 @@ QModelIndex FileSystemItemModel::counterpart(const QModelIndex &index, int colum
int FileSystemItemModel::rowCount(const QModelIndex &parent) const int FileSystemItemModel::rowCount(const QModelIndex &parent) const
{ {
if(FileSystemItem *parentItem = parent.isValid() if(const FileSystemItem *parentItem = (parent.isValid()
? reinterpret_cast<FileSystemItem *>(parent.internalPointer()) ? reinterpret_cast<FileSystemItem *>(parent.internalPointer())
: m_rootItem) { : m_rootItem)) {
return parentItem->children().length(); return parentItem->children().size();
} else { } else {
return 0; return 0;
} }
@ -264,10 +238,10 @@ int FileSystemItemModel::rowCount(const QModelIndex &parent) const
bool FileSystemItemModel::hasChildren(const QModelIndex &parent) const bool FileSystemItemModel::hasChildren(const QModelIndex &parent) const
{ {
if(FileSystemItem *parentItem = parent.isValid() if(const FileSystemItem *parentItem = (parent.isValid()
? reinterpret_cast<FileSystemItem *>(parent.internalPointer()) ? reinterpret_cast<const FileSystemItem *>(parent.internalPointer())
: m_rootItem) { : m_rootItem)) {
return parentItem->children().length() > 0; return parentItem->children().size() > 0;
} else { } else {
return false; return false;
} }

View File

@ -23,7 +23,6 @@ public:
void setRootItem(FileSystemItem *rootItem); void setRootItem(FileSystemItem *rootItem);
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role); bool setData(const QModelIndex &index, const QVariant &value, int role);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const; int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, QModelIndex index(int row, int column,