2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <cassert>
|
2023-06-30 23:04:16 +00:00
|
|
|
#include <queue>
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <QHeaderView>
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <QMenu>
|
2015-06-10 21:27:11 +00:00
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <libtransmission/transmission.h> // priorities
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FileTreeDelegate.h"
|
2015-06-15 21:07:46 +00:00
|
|
|
#include "FileTreeItem.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FileTreeModel.h"
|
|
|
|
#include "FileTreeView.h"
|
2015-06-15 21:07:46 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "Utils.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
char const* const PriorityKey = "priority";
|
|
|
|
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FileTreeView::FileTreeView(QWidget* parent, bool is_editable)
|
2023-07-18 15:20:17 +00:00
|
|
|
: QTreeView{ parent }
|
|
|
|
, model_{ new FileTreeModel(this, is_editable) }
|
|
|
|
, proxy_{ new QSortFilterProxyModel(this) }
|
|
|
|
, delegate_{ new FileTreeDelegate(this) }
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
proxy_->setSourceModel(model_);
|
|
|
|
proxy_->setSortRole(FileTreeModel::SortRole);
|
|
|
|
proxy_->setSortCaseSensitivity(Qt::CaseInsensitive);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
setModel(proxy_);
|
|
|
|
setItemDelegate(delegate_);
|
2017-04-19 12:04:45 +00:00
|
|
|
sortByColumn(FileTreeModel::COL_NAME, Qt::AscendingOrder);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(this, &QAbstractItemView::clicked, this, &FileTreeView::onClicked);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(model_, &FileTreeModel::openRequested, this, &FileTreeView::openRequested);
|
|
|
|
connect(model_, &FileTreeModel::pathEdited, this, &FileTreeView::pathEdited);
|
|
|
|
connect(model_, &FileTreeModel::priorityChanged, this, &FileTreeView::priorityChanged);
|
|
|
|
connect(model_, &FileTreeModel::wantedChanged, this, &FileTreeView::wantedChanged);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void FileTreeView::onClicked(QModelIndex const& proxy_index)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const model_index = proxy_->mapToSource(proxy_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (model_index.column() == FileTreeModel::COL_WANTED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddleWanted(QModelIndexList() << model_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (model_index.column() == FileTreeModel::COL_PRIORITY)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->twiddlePriority(QModelIndexList() << model_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::resizeEvent(QResizeEvent* event)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::resizeEvent(event);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// this is kind of a hack to get the last four columns be the
|
|
|
|
// right size, and to have the filename column use whatever
|
|
|
|
// space is left over...
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int left = event->size().width() - 1;
|
|
|
|
|
|
|
|
for (int column = 0; column < FileTreeModel::NUM_COLUMNS; ++column)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (column == FileTreeModel::COL_NAME)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int min_width = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QStringList item_texts;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
switch (column)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_SIZE:
|
2020-08-15 15:42:51 +00:00
|
|
|
for (int s = Formatter::get().B; s <= Formatter::get().TB; ++s)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
item_texts
|
|
|
|
<< (QStringLiteral("999.9 ") + Formatter::get().unitStr(Formatter::MEM, static_cast<Formatter::Size>(s)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PROGRESS:
|
2020-05-29 17:40:07 +00:00
|
|
|
item_texts << QStringLiteral(" 100% ");
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_WANTED:
|
2020-05-27 21:53:12 +00:00
|
|
|
min_width = 20;
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case FileTreeModel::COL_PRIORITY:
|
2021-08-15 09:41:48 +00:00
|
|
|
item_texts << FileTreeItem::tr("Low") << FileTreeItem::tr("Normal") << FileTreeItem::tr("High")
|
|
|
|
<< FileTreeItem::tr("Mixed");
|
2015-06-15 21:07:46 +00:00
|
|
|
break;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int item_width = 0;
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QString const& item_text : item_texts)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
item_width = std::max(item_width, Utils::measureViewItem(this, item_text));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QString const header_text = model_->headerData(column, Qt::Horizontal).toString();
|
2022-07-27 14:03:13 +00:00
|
|
|
int const header_width = Utils::measureHeaderItem(this->header(), header_text);
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int const width = std::max(min_width, std::max(item_width, header_width));
|
2017-04-19 12:04:45 +00:00
|
|
|
setColumnWidth(column, width);
|
|
|
|
|
|
|
|
left -= width;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setColumnWidth(FileTreeModel::COL_NAME, std::max(left, 0));
|
2015-06-15 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::keyPressEvent(QKeyEvent* event)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
if ((state() != EditingState) && (event->key() == Qt::Key_Space))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
// handle using the keyboard to toggle the
|
|
|
|
// wanted/unwanted state or the file priority
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
Qt::KeyboardModifiers const modifiers = event->modifiers();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
if (modifiers == Qt::NoModifier)
|
|
|
|
{
|
|
|
|
model_->twiddleWanted(selectedSourceRows());
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
if (modifiers == Qt::ShiftModifier)
|
|
|
|
{
|
|
|
|
model_->twiddlePriority(selectedSourceRows());
|
|
|
|
return;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::keyPressEvent(event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::mouseDoubleClickEvent(QMouseEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2022-11-15 16:25:12 +00:00
|
|
|
if (auto const index = currentIndex();
|
|
|
|
!index.isValid() || index.column() == FileTreeModel::COL_WANTED || index.column() == FileTreeModel::COL_PRIORITY)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (openSelectedItem())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::mouseDoubleClickEvent(event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::contextMenuEvent(QContextMenuEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2022-11-15 16:25:12 +00:00
|
|
|
if (auto const root_index = model_->index(0, 0); !root_index.isValid())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (context_menu_ == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
initContextMenu();
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->popup(event->globalPos());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void FileTreeView::update(FileList const& files, bool update_fields)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const model_was_empty = proxy_->rowCount() == 0;
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (TorrentFile const& file : files)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->addFile(file.index, file.filename, file.wanted, file.priority, file.size, file.have, update_fields);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (model_was_empty)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
expand(proxy_->index(0, 0));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
proxy_->sort(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::clear()
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->clear();
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::setEditable(bool editable)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->setEditable(editable);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool FileTreeView::edit(QModelIndex const& index, EditTrigger trigger, QEvent* event)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (selectionModel()->selectedRows().size() != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
QModelIndex const name_index = index.sibling(index.row(), FileTreeModel::COL_NAME);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (editTriggers().testFlag(trigger))
|
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
selectionModel()->setCurrentIndex(name_index, QItemSelectionModel::NoUpdate);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
return QTreeView::edit(name_index, trigger, event);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::checkSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->setWanted(selectedSourceRows(), true);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::uncheckSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->setWanted(selectedSourceRows(), false);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::onlyCheckSelectedItems()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex const root_index = model_->index(0, 0);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!root_index.isValid())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndexList wanted_indices = selectedSourceRows();
|
|
|
|
model_->setWanted(wanted_indices, true);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
std::sort(wanted_indices.begin(), wanted_indices.end());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QSet<QModelIndex> wanted_indices_parents;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QModelIndex const& i : wanted_indices)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (QModelIndex p = i.parent(); p.isValid(); p = p.parent())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
wanted_indices_parents.insert(p);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 23:04:16 +00:00
|
|
|
auto parents_queue = std::queue<QModelIndex>{};
|
|
|
|
parents_queue.emplace(root_index);
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndexList unwanted_indices;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-06-30 23:04:16 +00:00
|
|
|
while (!std::empty(parents_queue))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2023-07-01 15:22:15 +00:00
|
|
|
auto const parent_index = parents_queue.front();
|
2023-06-30 23:04:16 +00:00
|
|
|
parents_queue.pop();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (std::binary_search(wanted_indices.begin(), wanted_indices.end(), parent_index))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
auto const* parent_model = parent_index.model();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (int i = 0, count = model_->rowCount(parent_index); i < count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
QModelIndex const child_index = parent_model->index(i, 0, parent_index);
|
2020-05-27 21:53:12 +00:00
|
|
|
int const child_check_state = child_index.data(FileTreeModel::WantedRole).toInt();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (child_check_state == Qt::Unchecked ||
|
|
|
|
std::binary_search(wanted_indices.begin(), wanted_indices.end(), child_index))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
continue;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (child_check_state == Qt::Checked && child_index.data(FileTreeModel::FileIndexRole).toInt() >= 0)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
unwanted_indices << child_index;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-11-09 03:31:02 +00:00
|
|
|
else if (!wanted_indices_parents.contains(child_index))
|
|
|
|
{
|
|
|
|
unwanted_indices << child_index;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
2023-06-30 23:04:16 +00:00
|
|
|
parents_queue.emplace(child_index);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
model_->setWanted(unwanted_indices, false);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::setSelectedItemsPriority()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
auto const* action = qobject_cast<QAction const*>(sender());
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(action != nullptr);
|
2020-11-02 01:13:32 +00:00
|
|
|
model_->setPriority(selectedSourceRows(), action->property(PriorityKey).toInt());
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool FileTreeView::openSelectedItem()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return model_->openFile(proxy_->mapToSource(currentIndex()));
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::renameSelectedItem()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QTreeView::edit(currentIndex());
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::refreshContextMenuActionsSensitivity()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
assert(context_menu_ != nullptr);
|
|
|
|
|
|
|
|
QModelIndexList const selected_rows = selectionModel()->selectedRows();
|
|
|
|
Qt::CheckState const check_state = getCumulativeCheckState(selected_rows);
|
|
|
|
|
|
|
|
bool const have_selection = !selected_rows.isEmpty();
|
|
|
|
bool const have_single_selection = selected_rows.size() == 1;
|
|
|
|
bool const have_unchecked = check_state == Qt::Unchecked || check_state == Qt::PartiallyChecked;
|
|
|
|
bool const have_checked = check_state == Qt::Checked || check_state == Qt::PartiallyChecked;
|
|
|
|
|
|
|
|
check_selected_action_->setEnabled(have_unchecked);
|
|
|
|
uncheck_selected_action_->setEnabled(have_checked);
|
|
|
|
only_check_selected_action_->setEnabled(have_selection);
|
|
|
|
priority_menu_->setEnabled(have_selection);
|
2021-08-15 09:41:48 +00:00
|
|
|
open_action_->setEnabled(
|
|
|
|
have_single_selection && selected_rows.first().data(FileTreeModel::FileIndexRole).toInt() >= 0 &&
|
2020-05-27 21:53:12 +00:00
|
|
|
selected_rows.first().data(FileTreeModel::CompleteRole).toBool());
|
|
|
|
rename_action_->setEnabled(have_single_selection);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeView::initContextMenu()
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2023-07-18 15:20:17 +00:00
|
|
|
context_menu_ = new QMenu{ this };
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
check_selected_action_ = context_menu_->addAction(tr("Check Selected"), this, SLOT(checkSelectedItems()));
|
|
|
|
uncheck_selected_action_ = context_menu_->addAction(tr("Uncheck Selected"), this, SLOT(uncheckSelectedItems()));
|
|
|
|
only_check_selected_action_ = context_menu_->addAction(tr("Only Check Selected"), this, SLOT(onlyCheckSelectedItems()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
priority_menu_ = context_menu_->addMenu(tr("Priority"));
|
|
|
|
high_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("High"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
normal_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("Normal"), this, SLOT(setSelectedItemsPriority()));
|
|
|
|
low_priority_action_ = priority_menu_->addAction(FileTreeItem::tr("Low"), this, SLOT(setSelectedItemsPriority()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
high_priority_action_->setProperty(PriorityKey, TR_PRI_HIGH);
|
|
|
|
normal_priority_action_->setProperty(PriorityKey, TR_PRI_NORMAL);
|
|
|
|
low_priority_action_->setProperty(PriorityKey, TR_PRI_LOW);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
context_menu_->addSeparator();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
open_action_ = context_menu_->addAction(tr("Open"), this, SLOT(openSelectedItem()));
|
2022-10-11 15:39:41 +00:00
|
|
|
rename_action_ = context_menu_->addAction(tr("Rename…"), this, SLOT(renameSelectedItem()));
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(context_menu_, &QMenu::aboutToShow, this, &FileTreeView::refreshContextMenuActionsSensitivity);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList FileTreeView::selectedSourceRows(int column) const
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList indices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : selectionModel()->selectedRows(column))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
indices << proxy_->mapToSource(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2015-08-16 22:07:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
Qt::CheckState FileTreeView::getCumulativeCheckState(QModelIndexList const& indices)
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool have_checked = false;
|
|
|
|
bool have_unchecked = false;
|
2015-08-16 22:07:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : indices)
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (i.data(FileTreeModel::WantedRole).toInt())
|
2015-08-16 22:07:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::Checked:
|
2020-05-27 21:53:12 +00:00
|
|
|
have_checked = true;
|
2015-08-16 22:07:09 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::Unchecked:
|
2020-05-27 21:53:12 +00:00
|
|
|
have_unchecked = true;
|
2015-08-16 22:07:09 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case Qt::PartiallyChecked:
|
2015-08-16 22:07:09 +00:00
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (have_checked && have_unchecked)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
}
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return have_checked ? Qt::Checked : Qt::Unchecked;
|
2015-08-16 22:07:09 +00:00
|
|
|
}
|