mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
cart: support sorting by drag&drop or dialog
This commit is contained in:
parent
eb4d2a7ba3
commit
407658bead
@ -378,13 +378,11 @@ void VAttachmentList::sortItems()
|
||||
QTreeWidget *tree = dialog.getTreeWidget();
|
||||
tree->clear();
|
||||
tree->setColumnCount(1);
|
||||
QStringList headers;
|
||||
headers << tr("Name");
|
||||
QStringList headers(tr("Name"));
|
||||
tree->setHeaderLabels(headers);
|
||||
|
||||
for (int i = 0; i < attas.size(); ++i) {
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem(tree, QStringList(attas[i].m_name));
|
||||
|
||||
item->setData(0, Qt::UserRole, i);
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include "vmainwindow.h"
|
||||
#include "vnote.h"
|
||||
#include "vnotefile.h"
|
||||
#include "vlistwidget.h"
|
||||
#include "dialog/vsortdialog.h"
|
||||
|
||||
extern VMainWindow *g_mainWin;
|
||||
|
||||
@ -54,6 +56,7 @@ void VCart::setupUI()
|
||||
m_itemList->setAttribute(Qt::WA_MacShowFocusRect, false);
|
||||
m_itemList->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
m_itemList->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||
m_itemList->setDragDropMode(QAbstractItemView::InternalMove);
|
||||
connect(m_itemList, &QListWidget::customContextMenuRequested,
|
||||
this, &VCart::handleContextMenuRequested);
|
||||
connect(m_itemList, &QListWidget::itemActivated,
|
||||
@ -87,6 +90,13 @@ void VCart::initActions()
|
||||
m_deleteAct->setToolTip(tr("Delete selected items from Cart"));
|
||||
connect(m_deleteAct, &QAction::triggered,
|
||||
this, &VCart::deleteSelectedItems);
|
||||
|
||||
m_sortAct = new QAction(VIconUtils::menuIcon(":/resources/icons/sort.svg"),
|
||||
tr("&Sort"),
|
||||
this);
|
||||
m_sortAct->setToolTip(tr("Sort items in Cart"));
|
||||
connect(m_sortAct, &QAction::triggered,
|
||||
this, &VCart::sortItems);
|
||||
}
|
||||
|
||||
void VCart::handleContextMenuRequested(QPoint p_pos)
|
||||
@ -105,9 +115,17 @@ void VCart::handleContextMenuRequested(QPoint p_pos)
|
||||
menu.addAction(m_deleteAct);
|
||||
}
|
||||
|
||||
if (!menu.actions().isEmpty()) {
|
||||
menu.exec(m_itemList->mapToGlobal(p_pos));
|
||||
if (m_itemList->count() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!menu.actions().isEmpty()) {
|
||||
menu.addSeparator();
|
||||
}
|
||||
|
||||
menu.addAction(m_sortAct);
|
||||
|
||||
menu.exec(m_itemList->mapToGlobal(p_pos));
|
||||
}
|
||||
|
||||
void VCart::addFile(const QString &p_filePath)
|
||||
@ -207,3 +225,41 @@ QVector<QString> VCart::getFiles() const
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
void VCart::sortItems()
|
||||
{
|
||||
if (m_itemList->count() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
VSortDialog dialog(tr("Sort Cart"),
|
||||
tr("Sort items in Cart."),
|
||||
this);
|
||||
QTreeWidget *tree = dialog.getTreeWidget();
|
||||
tree->clear();
|
||||
tree->setColumnCount(1);
|
||||
QStringList headers(tr("Name"));
|
||||
tree->setHeaderLabels(headers);
|
||||
|
||||
int cnt = m_itemList->count();
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
QListWidgetItem *it = m_itemList->item(i);
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem(tree,
|
||||
QStringList(it->text()));
|
||||
item->setToolTip(0, getFilePath(it));
|
||||
item->setData(0, Qt::UserRole, i);
|
||||
}
|
||||
|
||||
dialog.treeUpdated();
|
||||
|
||||
if (dialog.exec()) {
|
||||
QVector<QVariant> data = dialog.getSortedData();
|
||||
Q_ASSERT(data.size() == cnt);
|
||||
QVector<int> sortedIdx(data.size(), -1);
|
||||
for (int i = 0; i < data.size(); ++i) {
|
||||
sortedIdx[i] = data[i].toInt();
|
||||
}
|
||||
|
||||
VListWidget::sortListWidget(m_itemList, sortedIdx);
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ private slots:
|
||||
|
||||
void locateCurrentItem();
|
||||
|
||||
void sortItems();
|
||||
|
||||
private:
|
||||
void setupUI();
|
||||
|
||||
@ -56,6 +58,7 @@ private:
|
||||
QAction *m_openAct;
|
||||
QAction *m_locateAct;
|
||||
QAction *m_deleteAct;
|
||||
QAction *m_sortAct;
|
||||
};
|
||||
|
||||
#endif // VCART_H
|
||||
|
@ -142,3 +142,19 @@ void VListWidget::selectNextItem(bool p_forward)
|
||||
Q_UNUSED(p_forward);
|
||||
Q_ASSERT(false);
|
||||
}
|
||||
|
||||
void VListWidget::sortListWidget(QListWidget *p_list, const QVector<int> &p_sortedIdx)
|
||||
{
|
||||
int cnt = p_list->count();
|
||||
Q_ASSERT(cnt == p_sortedIdx.size());
|
||||
|
||||
QVector<QListWidgetItem *> sortedItems(cnt);
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
sortedItems[i] = p_list->item(p_sortedIdx[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
QListWidgetItem *it = p_list->takeItem(p_list->row(sortedItems[i]));
|
||||
p_list->insertItem(i, it);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define VLISTWIDGET_H
|
||||
|
||||
#include <QListWidget>
|
||||
#include <QVector>
|
||||
|
||||
#include "vsimplesearchinput.h"
|
||||
|
||||
@ -31,6 +32,9 @@ public:
|
||||
|
||||
virtual void selectNextItem(bool p_forward) Q_DECL_OVERRIDE;
|
||||
|
||||
// Sort @p_list according to @p_sortedIdx.
|
||||
static void sortListWidget(QListWidget *p_list, const QVector<int> &p_sortedIdx);
|
||||
|
||||
private slots:
|
||||
void handleSearchModeTriggered(bool p_inSearchMode);
|
||||
|
||||
|
@ -99,6 +99,7 @@ void VSearcher::setupUI()
|
||||
m_keywordCB->setEditable(true);
|
||||
m_keywordCB->setLineEdit(new VLineEdit(this));
|
||||
m_keywordCB->setToolTip(tr("Keywords to search for"));
|
||||
m_keywordCB->lineEdit()->setPlaceholderText(tr("Supports space, &&, and ||"));
|
||||
connect(m_keywordCB, &QComboBox::currentTextChanged,
|
||||
this, &VSearcher::handleInputChanged);
|
||||
connect(m_keywordCB->lineEdit(), &QLineEdit::returnPressed,
|
||||
|
Loading…
x
Reference in New Issue
Block a user