108 lines
3.2 KiB
C++
108 lines
3.2 KiB
C++
|
#include "FIFOContainer.h"
|
||
|
#include <iostream>
|
||
|
#include <QFileInfo>
|
||
|
using Json = nlohmann::json;
|
||
|
|
||
|
|
||
|
FIFOContainer::FIFOContainer(size_t max_size) : m_max_size(max_size) {}
|
||
|
|
||
|
void FIFOContainer::addRecord(const QString& filePath)
|
||
|
{
|
||
|
if (!filePath.isEmpty())
|
||
|
{
|
||
|
// 获取文件的完整路径
|
||
|
QFileInfo fileInfo(filePath);
|
||
|
QString filePath_get = fileInfo.filePath(); // 完整路径
|
||
|
QString fileName_get = fileInfo.fileName(); // 完整文件名(包括扩展名)
|
||
|
QStringPair pair = QStringPair(fileName_get, filePath_get);
|
||
|
|
||
|
|
||
|
if (m_queue.size() == m_max_size)
|
||
|
{
|
||
|
// 如果容器已满,移除队头(最早的记录)
|
||
|
m_queue.pop_front();
|
||
|
}
|
||
|
|
||
|
if (m_queue.empty())
|
||
|
{
|
||
|
m_queue.push_back(pair);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
for (auto it = m_queue.begin(); it != m_queue.end(); ++it)
|
||
|
{
|
||
|
if (it->second == pair.second)
|
||
|
{
|
||
|
// 如果已经存在,移除该元素并将其移到最后
|
||
|
m_queue.erase(it);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
m_queue.push_back(pair);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void FIFOContainer::displayRecords() const
|
||
|
{
|
||
|
// for (const QString& filePath : m_queue) {
|
||
|
// std::cout << filePath.toStdString() << std::endl;
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
void FIFOContainer::saveToFile(const QString& fileName) const
|
||
|
{
|
||
|
QSettings settings(fileName, QSettings::IniFormat); // 使用 INI 格式
|
||
|
settings.beginGroup("recent_open_file"); // 设置组
|
||
|
|
||
|
// 保存队列中的路径
|
||
|
for (int i = 0; i < m_queue.size(); ++i) {
|
||
|
settings.setValue("path" + QString::number(i), m_queue[i].second);
|
||
|
}
|
||
|
|
||
|
settings.endGroup(); // 结束组
|
||
|
}
|
||
|
|
||
|
void FIFOContainer::loadFromFile(const QString& fileName) {
|
||
|
QSettings settings(fileName, QSettings::IniFormat); // 使用 INI 格式
|
||
|
settings.beginGroup("recent_open_file"); // 设置组
|
||
|
|
||
|
m_queue.clear(); // 清空当前队列
|
||
|
|
||
|
// 读取所有的路径并加入队列
|
||
|
int i = 0;
|
||
|
while (settings.contains("path" + QString::number(i)))
|
||
|
{
|
||
|
QString filePath_read = settings.value("path" + QString::number(i)).toString();
|
||
|
|
||
|
if (!filePath_read.isEmpty()) {
|
||
|
// 获取文件的完整路径
|
||
|
QFileInfo fileInfo(filePath_read);
|
||
|
QString filePath = fileInfo.filePath(); // 完整路径
|
||
|
QString fileNameOnly = fileInfo.fileName(); // 完整文件名(包括扩展名)
|
||
|
QStringPair pair = QStringPair(fileNameOnly, filePath);
|
||
|
m_queue.push_back(pair);
|
||
|
}
|
||
|
++i;
|
||
|
}
|
||
|
settings.endGroup(); // 结束组
|
||
|
}
|
||
|
|
||
|
Json FIFOContainer::toJson() const
|
||
|
{
|
||
|
Json jsonobj = Json::object();
|
||
|
Json jsonArray = Json::array();
|
||
|
|
||
|
// 将队列中的所有 QString 转换为 std::string 并添加到 nlohmann::json 数组中
|
||
|
for (auto it = m_queue.rbegin(); it != m_queue.rend(); ++it)
|
||
|
{
|
||
|
Json json = Json::object();
|
||
|
json["name"] = it->first.toStdString();
|
||
|
json["path"] = it->second.toStdString();
|
||
|
jsonArray.push_back(json); // 将 QString 转换为 std::string
|
||
|
}
|
||
|
|
||
|
jsonobj["recent_files"] = jsonArray;
|
||
|
return jsonobj;
|
||
|
}
|