mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
support reordering notes in file list panel
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
c44928e1d8
commit
880d16e8f4
@ -582,3 +582,59 @@ void VDirectory::setExpanded(bool p_expanded)
|
|||||||
m_expanded = p_expanded;
|
m_expanded = p_expanded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VDirectory::reorderFiles(int p_first, int p_last, int p_destStart)
|
||||||
|
{
|
||||||
|
Q_ASSERT(m_opened);
|
||||||
|
Q_ASSERT(p_first <= p_last);
|
||||||
|
Q_ASSERT(p_last < m_files.size());
|
||||||
|
Q_ASSERT(p_destStart < p_first || p_destStart > p_last);
|
||||||
|
Q_ASSERT(p_destStart >= 0 && p_destStart <= m_files.size());
|
||||||
|
|
||||||
|
if (!reorderFilesInConfig(p_first, p_last, p_destStart)) {
|
||||||
|
qWarning() << "failed to reorder files in config" << p_first << p_last << p_destStart;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reorder m_files.
|
||||||
|
if (p_destStart > p_last) {
|
||||||
|
int to = p_destStart - 1;
|
||||||
|
for (int i = p_first; i <= p_last; ++i) {
|
||||||
|
// Move p_first to p_destStart every time.
|
||||||
|
m_files.move(p_first, to);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
int to = p_destStart;
|
||||||
|
for (int i = p_first; i <= p_last; ++i) {
|
||||||
|
m_files.move(i, to++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VDirectory::reorderFilesInConfig(int p_first, int p_last, int p_destStart)
|
||||||
|
{
|
||||||
|
QString path = retrivePath();
|
||||||
|
QJsonObject dirJson = VConfigManager::readDirectoryConfig(path);
|
||||||
|
Q_ASSERT(!dirJson.isEmpty());
|
||||||
|
QJsonArray fileArray = dirJson["files"].toArray();
|
||||||
|
Q_ASSERT(fileArray.size() == m_files.size());
|
||||||
|
|
||||||
|
if (p_destStart > p_last) {
|
||||||
|
int to = p_destStart - 1;
|
||||||
|
for (int i = p_first; i <= p_last; ++i) {
|
||||||
|
// Move p_first to p_destStart every time.
|
||||||
|
QJsonValue ele = fileArray.takeAt(p_first);
|
||||||
|
fileArray.insert(to, ele);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
int to = p_destStart;
|
||||||
|
for (int i = p_first; i <= p_last; ++i) {
|
||||||
|
QJsonValue ele = fileArray.takeAt(i);
|
||||||
|
fileArray.insert(to++, ele);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dirJson["files"] = fileArray;
|
||||||
|
if (!VConfigManager::writeDirectoryConfig(path, dirJson)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -59,6 +59,8 @@ public:
|
|||||||
inline QString getNotebookName() const;
|
inline QString getNotebookName() const;
|
||||||
inline bool isExpanded() const;
|
inline bool isExpanded() const;
|
||||||
void setExpanded(bool p_expanded);
|
void setExpanded(bool p_expanded);
|
||||||
|
void reorderFiles(int p_first, int p_last, int p_destStart);
|
||||||
|
bool reorderFilesInConfig(int p_first, int p_last, int p_destStart);
|
||||||
|
|
||||||
static QJsonObject createDirectoryJson();
|
static QJsonObject createDirectoryJson();
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ void VFileList::setupUI()
|
|||||||
fileList = new QListWidget(this);
|
fileList = new QListWidget(this);
|
||||||
fileList->setContextMenuPolicy(Qt::CustomContextMenu);
|
fileList->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||||
fileList->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
fileList->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||||
|
fileList->setDragDropMode(QAbstractItemView::InternalMove);
|
||||||
fileList->setObjectName("FileList");
|
fileList->setObjectName("FileList");
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||||
@ -31,6 +32,8 @@ void VFileList::setupUI()
|
|||||||
this, &VFileList::contextMenuRequested);
|
this, &VFileList::contextMenuRequested);
|
||||||
connect(fileList, &QListWidget::itemClicked,
|
connect(fileList, &QListWidget::itemClicked,
|
||||||
this, &VFileList::handleItemClicked);
|
this, &VFileList::handleItemClicked);
|
||||||
|
connect(fileList->model(), &QAbstractItemModel::rowsMoved,
|
||||||
|
this, &VFileList::handleRowsMoved);
|
||||||
|
|
||||||
setLayout(mainLayout);
|
setLayout(mainLayout);
|
||||||
}
|
}
|
||||||
@ -140,8 +143,9 @@ QListWidgetItem* VFileList::insertFileListItem(VFile *file, bool atFront)
|
|||||||
{
|
{
|
||||||
Q_ASSERT(file);
|
Q_ASSERT(file);
|
||||||
QListWidgetItem *item = new QListWidgetItem(file->getName());
|
QListWidgetItem *item = new QListWidgetItem(file->getName());
|
||||||
item->setData(Qt::UserRole, QVariant::fromValue(file));
|
unsigned long long ptr = (long long)file;
|
||||||
|
item->setData(Qt::UserRole, ptr);
|
||||||
|
Q_ASSERT(sizeof(file) <= sizeof(ptr));
|
||||||
if (atFront) {
|
if (atFront) {
|
||||||
fileList->insertItem(0, item);
|
fileList->insertItem(0, item);
|
||||||
} else {
|
} else {
|
||||||
@ -469,3 +473,27 @@ bool VFileList::locateFile(const VFile *p_file)
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VFileList::handleRowsMoved(const QModelIndex &p_parent, int p_start, int p_end, const QModelIndex &p_destination, int p_row)
|
||||||
|
{
|
||||||
|
if (p_parent == p_destination) {
|
||||||
|
// Items[p_start, p_end] are moved to p_row.
|
||||||
|
m_directory->reorderFiles(p_start, p_end, p_row);
|
||||||
|
Q_ASSERT(identicalListWithDirectory());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VFileList::identicalListWithDirectory() const
|
||||||
|
{
|
||||||
|
const QVector<VFile *> files = m_directory->getFiles();
|
||||||
|
int nrItems = fileList->count();
|
||||||
|
if (nrItems != files.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nrItems; ++i) {
|
||||||
|
if (getVFile(fileList->item(i)) != files.at(i)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -44,6 +44,7 @@ private slots:
|
|||||||
void cutSelectedFiles();
|
void cutSelectedFiles();
|
||||||
void pasteFilesInCurDir();
|
void pasteFilesInCurDir();
|
||||||
void deleteFile();
|
void deleteFile();
|
||||||
|
void handleRowsMoved(const QModelIndex &p_parent, int p_start, int p_end, const QModelIndex &p_destination, int p_row);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setDirectory(VDirectory *p_directory);
|
void setDirectory(VDirectory *p_directory);
|
||||||
@ -64,7 +65,9 @@ private:
|
|||||||
bool copyFile(VDirectory *p_destDir, const QString &p_destName, VFile *p_file, bool p_cut);
|
bool copyFile(VDirectory *p_destDir, const QString &p_destName, VFile *p_file, bool p_cut);
|
||||||
// New items have been added to direcotry. Update file list accordingly.
|
// New items have been added to direcotry. Update file list accordingly.
|
||||||
QVector<QListWidgetItem *> updateFileListAdded();
|
QVector<QListWidgetItem *> updateFileListAdded();
|
||||||
inline QPointer<VFile> getVFile(QListWidgetItem *p_item);
|
inline QPointer<VFile> getVFile(QListWidgetItem *p_item) const;
|
||||||
|
// Check if the list items match exactly the contents of the directory.
|
||||||
|
bool identicalListWithDirectory() const;
|
||||||
|
|
||||||
VEditArea *editArea;
|
VEditArea *editArea;
|
||||||
QListWidget *fileList;
|
QListWidget *fileList;
|
||||||
@ -85,10 +88,10 @@ inline void VFileList::setEditArea(VEditArea *editArea)
|
|||||||
this->editArea = editArea;
|
this->editArea = editArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline QPointer<VFile> VFileList::getVFile(QListWidgetItem *p_item)
|
inline QPointer<VFile> VFileList::getVFile(QListWidgetItem *p_item) const
|
||||||
{
|
{
|
||||||
Q_ASSERT(p_item);
|
Q_ASSERT(p_item);
|
||||||
return p_item->data(Qt::UserRole).value<VFile *>();
|
return (VFile *)p_item->data(Qt::UserRole).toULongLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const VDirectory *VFileList::currentDirectory() const
|
inline const VDirectory *VFileList::currentDirectory() const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user