275 lines
6.5 KiB
C++
275 lines
6.5 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 = "Plain Text";
|
|
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 "Plain Text";
|
|
}
|
|
|
|
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];
|
|
if (doc.content != content) {
|
|
doc.content = content;
|
|
if (!doc.modified) {
|
|
doc.modified = true;
|
|
emit documentModified(docId, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
QString extension = fileInfo.suffix().toLower();
|
|
|
|
if (extension == "cpp" || extension == "cxx" || extension == "cc" || extension == "c++") {
|
|
return "C++";
|
|
} else if (extension == "c") {
|
|
return "C";
|
|
} else if (extension == "h" || extension == "hpp" || extension == "hxx") {
|
|
return "C/C++ Header";
|
|
} else if (extension == "py") {
|
|
return "Python";
|
|
} else if (extension == "js") {
|
|
return "JavaScript";
|
|
} else if (extension == "html" || extension == "htm") {
|
|
return "HTML";
|
|
} else if (extension == "css") {
|
|
return "CSS";
|
|
} else if (extension == "xml") {
|
|
return "XML";
|
|
} else if (extension == "json") {
|
|
return "JSON";
|
|
} else if (extension == "txt") {
|
|
return "Plain Text";
|
|
} else {
|
|
return "Plain Text";
|
|
}
|
|
} |