Skip to content

Commit

Permalink
Add command line option for recursive opening, add command line help …
Browse files Browse the repository at this point in the history
…message.
  • Loading branch information
0xb8 committed Aug 10, 2022
1 parent 9c95f48 commit ad31c8c
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 180 deletions.
22 changes: 11 additions & 11 deletions resources/html/English/welcome.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<html>
<body>
<div align="center">
<div style="font-size:20px">Open image</div>
<table>
<tr>
<td>
<hr/>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;File > <a style="color: #0089cc" href="#open_file">Open File</a></div>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;File > <a style="color: #0089cc" href="#open_dir">Open Directory</a></div>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;Drag and drop files or directories here</div>
</td>
</tr>
</table>
<div style="font-size:20px">Open image</div>
<table>
<tr>
<td>
<hr/>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;File > <a style="color: #0089cc" href="#open_file">Open File</a></div>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;File > <a style="color: #0089cc" href="#open_dir">Open Folder</a></div>
<div style="margin-top: 5px">&bull;&nbsp;&nbsp;Drag and drop files or folders here</div>
</td>
</tr>
</table>
</div>
</body>
</html>
155 changes: 85 additions & 70 deletions resources/i18n/English.ts

Large diffs are not rendered by default.

Binary file modified resources/i18n/Russian.qm
Binary file not shown.
155 changes: 85 additions & 70 deletions resources/i18n/Russian.ts

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions src/file_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void FileQueue::push(const QString &f, bool recursive)
}
}

void FileQueue::assign(const QStringList& paths)
void FileQueue::assign(const QStringList& paths, bool recursive)
{
#if __GNUC__ > 5 // NOTE: workaround for old gcc used by appveyor
static_assert(util::traits::is_nothrow_swappable_all_v<decltype(m_files)>, "Member container should be nothrow swappable");
Expand All @@ -125,17 +125,32 @@ void FileQueue::assign(const QStringList& paths)
using cont_t = decltype(m_files);
cont_t tmp;
QFileInfo fi;

int count = 0;
auto update_ui = [&count]() {
if (count++ > 2500) {
// avoid gui hang
qApp->processEvents();
count = 0;
}
};

for(const auto & p : qAsConst(paths)) {
fi.setFile(p);
if(fi.isFile()) {
tmp.push_back(fi.absoluteFilePath());
}
if(fi.isDir()) {
QDirIterator it(p, m_name_filters, QDir::Files);
QDirIterator it(p, m_name_filters,
QDir::Files,
recursive ? QDirIterator::Subdirectories | QDirIterator::FollowSymlinks
: QDirIterator::NoIteratorFlags);
while(it.hasNext() && !it.next().isNull()) {
tmp.push_back(it.fileInfo().absoluteFilePath());
update_ui();
}
}
update_ui();
}

std::swap(tmp, m_files);
Expand Down
6 changes: 4 additions & 2 deletions src/file_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,18 @@ class FileQueue {
*
* \note This function provides basic exception safety guarantee.
*/
void push(const QString& path, bool recursive=false);
void push(const QString& path, bool recursive);



/*!
* \brief Replace queue contents with list of \p paths.
* \param recursive When a directory is found in \p paths, enqueue files
* in all subdirectories as well.
*
* \note This function provides strong exception safety guarantee.
*/
void assign(const QStringList& paths);
void assign(const QStringList& paths, bool recursive);


/*!
Expand Down
10 changes: 5 additions & 5 deletions src/tagger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,18 @@ void Tagger::clear()
emit cleared();
}

bool Tagger::open(const QString& filename)
bool Tagger::open(const QString& filename, bool recursive)
{
if(rename() == RenameStatus::Cancelled)
return false;

bool res = openDir(filename, false);
bool res = openDir(filename, recursive);
if(!res) res = openSession(filename);
if(!res) res = openFile(filename);
if(!res) res = openFile(filename, recursive);
return res;
}

bool Tagger::openFile(const QString &filename)
bool Tagger::openFile(const QString &filename, bool recursive)
{
if(rename() == RenameStatus::Cancelled)
return false;
Expand All @@ -134,7 +134,7 @@ bool Tagger::openFile(const QString &filename)
}
qApp->setOverrideCursor(Qt::WaitCursor);
m_file_queue.clear();
m_file_queue.push(fi.absolutePath());
m_file_queue.push(fi.absolutePath(), recursive);
m_file_queue.sort();
m_file_queue.select(m_file_queue.find(fi.absoluteFilePath()));
m_picture.cache.clear();
Expand Down
6 changes: 3 additions & 3 deletions src/tagger.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ class Tagger : public QWidget
~Tagger() override = default;

/// Open file, session or directory.
bool open(const QString& filename);
bool open(const QString& filename, bool recursive);

/// Open file and enqueue its directory.
bool openFile(const QString&);
bool openFile(const QString& filename, bool recursive);

/// Enqueue directory contents and open first file.
bool openDir(const QString&, bool recursive);
bool openDir(const QString& dirname, bool recursive);

/// Open tagging session from file.
bool openSession(const QString& sfile);
Expand Down
54 changes: 37 additions & 17 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,18 @@ void Window::fileOpenDialog()
.arg(util::join(supported_video_formats)));

if(fileNames.size() == 1) {
m_tagger.open(fileNames.first());
m_tagger.open(fileNames.first(), /*recursive=*/false);
}
if(fileNames.size() > 1) {
m_tagger.queue().assign(fileNames);
m_tagger.queue().assign(fileNames, /*recursive=*/false);
m_tagger.openFileInQueue();
}
}

void Window::directoryOpenDialog(bool recursive)
{
auto dir = QFileDialog::getExistingDirectory(nullptr,
tr("Open Directory"),
recursive ? tr("Open Folder Recursively") : tr("Open Folder"),
m_last_directory,
QFileDialog::ShowDirsOnly);

Expand Down Expand Up @@ -502,6 +502,7 @@ bool Window::eventFilter(QObject*, QEvent *e)
return true;
}
if(e->type() == QEvent::Drop) {
bool recursive_enabled = qApp->keyboardModifiers() & Qt::SHIFT;
const auto drop_event = static_cast<QDropEvent*>(e);
const auto fileurls = drop_event->mimeData()->urls();
QFileInfo dropfile;
Expand All @@ -510,7 +511,7 @@ bool Window::eventFilter(QObject*, QEvent *e)

if(fileurls.size() == 1) {
dropfile.setFile(fileurls.first().toLocalFile());
m_tagger.open(dropfile.absoluteFilePath());
m_tagger.open(dropfile.absoluteFilePath(), recursive_enabled);
return true;
}

Expand All @@ -522,7 +523,7 @@ bool Window::eventFilter(QObject*, QEvent *e)
for(const auto& fileurl : qAsConst(fileurls)) {
filelist.push_back(fileurl.toLocalFile());
}
m_tagger.queue().assign(filelist);
m_tagger.queue().assign(filelist, recursive_enabled);
m_tagger.openFileInQueue();
return true;
}
Expand Down Expand Up @@ -603,24 +604,43 @@ void Window::parseCommandLineArguments()
if(args.size() <= 1)
return;

if(args.size() == 2) {
if (args[1] == QStringLiteral("--restart")) {
readRestartData();
return;
}

m_tagger.open(args.back()); // may open session file or read list from stdin
return;
}
QStringList paths;
bool recursive = false;

for(int i = 1; i < args.size(); ++i) {
auto& arg = args.at(i);
if(args.startsWith(QChar('-'))) {
const auto& arg = args.at(i);
if(arg.startsWith(QChar('-'))) {
if (arg == QStringLiteral("--restart")) {
readRestartData();
return;
}

if (arg == QStringLiteral("-h") || arg == QStringLiteral("--help")) {
QMessageBox::information(this,
tr("%1 Command Line Usage").arg(TARGET_PRODUCT),
tr("<h4><pre>%1 [-r | --recursive] [-h | --help] [path...]</pre></h4>"
"<table>"
"<tr><td style=\"padding: 5px 10px\"><code>-r, --recursive</code></td><td style=\"padding: 5px 10px\">Open files in subdirectories recursively.</td></tr>"
"<tr><td style=\"padding: 5px 10px\"><code>-h, --help</code></td><td style=\"padding: 5px 10px\">Display this help message.</td></tr>"
"<tr><td style=\"padding: 5px 10px\"><code>path...</code></td><td style=\"padding: 5px 10px\">One or several file/directory paths.<br/>Use \"-\" to read paths from <code>stdin</code>.</td></tr>"
"</table>").arg(TARGET_PRODUCT));
}

if (arg == QStringLiteral("-r") || arg == QStringLiteral("--recursive")) {
recursive = true;
}

continue;
}
m_tagger.queue().push(arg);
paths.append(arg);
}

if (paths.size() == 1) {
m_tagger.open(paths[0], recursive); // may open session file or read list from stdin
return;
}

m_tagger.queue().assign(paths, recursive);
m_tagger.queue().select(0); // must select 0 before sorting to always open first argument
m_tagger.queue().sort();
m_tagger.openFileInQueue(m_tagger.queue().currentIndex()); // sorting changed the index of selected file
Expand Down

0 comments on commit ad31c8c

Please sign in to comment.