292 lines
7.1 KiB
C++
292 lines
7.1 KiB
C++
#include "documentmanager.h"
|
|
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QTextStream>
|
|
#include <QDebug>
|
|
|
|
int DocumentManager::s_untitledCounter = 1;
|
|
|
|
DocumentManager::DocumentManager(QObject *parent)
|
|
: QObject(parent)
|
|
, m_nextDocumentId(1)
|
|
{
|
|
}
|
|
|
|
DocumentManager::~DocumentManager()
|
|
{
|
|
}
|
|
|
|
int DocumentManager::createNewDocument()
|
|
{
|
|
DocumentInfo doc;
|
|
doc.id = m_nextDocumentId++;
|
|
doc.title = generateUntitledName();
|
|
doc.filePath = QString();
|
|
doc.content = QString();
|
|
doc.encoding = "UTF-8";
|
|
doc.language = QString();
|
|
doc.modified = false;
|
|
doc.untitled = true;
|
|
|
|
m_documents[doc.id] = doc;
|
|
|
|
emit documentCreated(doc.id);
|
|
return doc.id;
|
|
}
|
|
|
|
int DocumentManager::openDocument(const QString &filePath)
|
|
{
|
|
QFile file(filePath);
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
qWarning() << "Failed to open file:" << filePath;
|
|
return -1;
|
|
}
|
|
|
|
QTextStream in(&file);
|
|
in.setEncoding(QStringConverter::Utf8);
|
|
QString content = in.readAll();
|
|
file.close();
|
|
|
|
DocumentInfo doc;
|
|
doc.id = m_nextDocumentId++;
|
|
doc.title = QFileInfo(filePath).fileName();
|
|
doc.filePath = filePath;
|
|
doc.content = content;
|
|
doc.encoding = "UTF-8";
|
|
doc.language = detectLanguageFromExtension(filePath);
|
|
doc.modified = false;
|
|
doc.untitled = false;
|
|
|
|
m_documents[doc.id] = doc;
|
|
|
|
emit documentOpened(doc.id, filePath);
|
|
return doc.id;
|
|
}
|
|
|
|
bool DocumentManager::saveDocument(int docId)
|
|
{
|
|
if (!m_documents.contains(docId)) {
|
|
return false;
|
|
}
|
|
|
|
DocumentInfo &doc = m_documents[docId];
|
|
if (doc.untitled || doc.filePath.isEmpty()) {
|
|
return false; // 需要使用 saveAs
|
|
}
|
|
|
|
QFile file(doc.filePath);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
qWarning() << "Failed to save file:" << doc.filePath;
|
|
return false;
|
|
}
|
|
|
|
QTextStream out(&file);
|
|
out.setEncoding(QStringConverter::Utf8);
|
|
out << doc.content;
|
|
file.close();
|
|
|
|
doc.modified = false;
|
|
emit documentSaved(docId, doc.filePath);
|
|
emit documentModified(docId, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DocumentManager::saveDocumentAs(int docId, const QString &filePath)
|
|
{
|
|
if (!m_documents.contains(docId)) {
|
|
return false;
|
|
}
|
|
|
|
DocumentInfo &doc = m_documents[docId];
|
|
|
|
QFile file(filePath);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
qWarning() << "Failed to save file as:" << filePath;
|
|
return false;
|
|
}
|
|
|
|
QTextStream out(&file);
|
|
out.setEncoding(QStringConverter::Utf8);
|
|
out << doc.content;
|
|
file.close();
|
|
|
|
// 更新文档信息
|
|
doc.filePath = filePath;
|
|
doc.title = QFileInfo(filePath).fileName();
|
|
doc.language = detectLanguageFromExtension(filePath);
|
|
doc.modified = false;
|
|
doc.untitled = false;
|
|
|
|
emit documentSaved(docId, filePath);
|
|
emit documentModified(docId, false);
|
|
emit documentTitleChanged(docId, doc.title);
|
|
|
|
return true;
|
|
}
|
|
|
|
void DocumentManager::closeDocument(int docId)
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
m_documents.remove(docId);
|
|
emit documentClosed(docId);
|
|
}
|
|
}
|
|
|
|
QString DocumentManager::getDocumentTitle(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].title;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QString DocumentManager::getDocumentFilePath(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].filePath;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QString DocumentManager::getDocumentContent(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].content;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QString DocumentManager::getDocumentEncoding(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].encoding;
|
|
}
|
|
return "UTF-8";
|
|
}
|
|
|
|
QString DocumentManager::getDocumentLanguage(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].language;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
bool DocumentManager::isDocumentModified(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].modified;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool DocumentManager::isDocumentUntitled(int docId) const
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
return m_documents[docId].untitled;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void DocumentManager::setDocumentContent(int docId, const QString &content)
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
DocumentInfo &doc = m_documents[docId];
|
|
doc.content = content;
|
|
}
|
|
}
|
|
|
|
void DocumentManager::setDocumentModified(int docId, bool modified)
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
DocumentInfo &doc = m_documents[docId];
|
|
if (doc.modified != modified) {
|
|
doc.modified = modified;
|
|
emit documentModified(docId, modified);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DocumentManager::setDocumentEncoding(int docId, const QString &encoding)
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
m_documents[docId].encoding = encoding;
|
|
}
|
|
}
|
|
|
|
void DocumentManager::setDocumentLanguage(int docId, const QString &language)
|
|
{
|
|
if (m_documents.contains(docId)) {
|
|
m_documents[docId].language = language;
|
|
}
|
|
}
|
|
|
|
QList<int> DocumentManager::getAllDocumentIds() const
|
|
{
|
|
return m_documents.keys();
|
|
}
|
|
|
|
int DocumentManager::getDocumentCount() const
|
|
{
|
|
return m_documents.size();
|
|
}
|
|
|
|
QString DocumentManager::generateUntitledName()
|
|
{
|
|
return QString("Untitled-%1").arg(s_untitledCounter++);
|
|
}
|
|
|
|
QString DocumentManager::detectLanguageFromExtension(const QString &filePath)
|
|
{
|
|
QFileInfo fileInfo(filePath);
|
|
const QString fileName = fileInfo.fileName();
|
|
const QString extension = fileInfo.suffix().toLower();
|
|
|
|
if (fileName.compare(QStringLiteral("CMakeLists.txt"), Qt::CaseInsensitive) == 0) {
|
|
return QStringLiteral("cmake");
|
|
}
|
|
|
|
if (extension == QStringLiteral("cpp") || extension == QStringLiteral("cxx") ||
|
|
extension == QStringLiteral("cc") || extension == QStringLiteral("c++") ||
|
|
extension == QStringLiteral("c") || extension == QStringLiteral("h") ||
|
|
extension == QStringLiteral("hpp") || extension == QStringLiteral("hxx")) {
|
|
return QStringLiteral("cpp");
|
|
}
|
|
|
|
if (extension == QStringLiteral("py")) {
|
|
return QStringLiteral("python");
|
|
}
|
|
|
|
if (extension == QStringLiteral("html") || extension == QStringLiteral("htm")) {
|
|
return QStringLiteral("hypertext");
|
|
}
|
|
|
|
if (extension == QStringLiteral("xml") || extension == QStringLiteral("xsd") || extension == QStringLiteral("xsl")) {
|
|
return QStringLiteral("xml");
|
|
}
|
|
|
|
if (extension == QStringLiteral("json")) {
|
|
return QStringLiteral("json");
|
|
}
|
|
|
|
if (extension == QStringLiteral("css")) {
|
|
return QStringLiteral("css");
|
|
}
|
|
|
|
if (extension == QStringLiteral("cmake")) {
|
|
return QStringLiteral("cmake");
|
|
}
|
|
|
|
if (extension == QStringLiteral("yaml") || extension == QStringLiteral("yml")) {
|
|
return QStringLiteral("yaml");
|
|
}
|
|
|
|
if (extension == QStringLiteral("ps1") || extension == QStringLiteral("psm1") || extension == QStringLiteral("psd1")) {
|
|
return QStringLiteral("powershell");
|
|
}
|
|
|
|
return QString();
|
|
}
|