Skip to content

Commit f5dc611

Browse files
committed
liteeditor: enable open binary file on editor. editor widget readonly set only for binary file (ignore text file).
1 parent f2525d6 commit f5dc611

File tree

6 files changed

+83
-68
lines changed

6 files changed

+83
-68
lines changed

liteidex/src/api/liteapi/liteapi.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class IFile : public QObject
173173
virtual bool reloadText(QString &outText) = 0;
174174
virtual bool saveText(const QString &filePath, const QString &text) = 0;
175175
virtual bool isReadOnly() const = 0;
176+
virtual bool isBinary() const = 0;
176177
virtual QString filePath() const = 0;
177178
virtual QString mimeType() const = 0;
178179
};
@@ -939,7 +940,7 @@ inline QString findPackageByMimeType(LiteApi::IApplication *app, const QString m
939940

940941
} //namespace LiteApi
941942

942-
Q_DECLARE_INTERFACE(LiteApi::IPluginFactory,"LiteApi.IPluginFactory.X33.2")
943+
Q_DECLARE_INTERFACE(LiteApi::IPluginFactory,"LiteApi.IPluginFactory.X34")
943944

944945

945946
#endif //LITEAPI_H

liteidex/src/plugins/liteeditor/liteeditor.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ void LiteEditor::setReadOnly(bool b)
868868
m_lockAct->setVisible(b);
869869
m_unlockAct->setVisible(!b);
870870
m_bReadOnly = b;
871-
m_editorWidget->setReadOnly(b);
871+
m_editorWidget->setReadOnly(m_file->isBinary());
872+
// m_editorWidget->setReadOnly(b);
872873
}
873874

874875
bool LiteEditor::isReadOnly() const

liteidex/src/plugins/liteeditor/liteeditorfile.cpp

+71-66
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ LiteEditorFile::LiteEditorFile(LiteApi::IApplication *app, QObject *parent)
5050
m_codec = QTextCodec::codecForName("utf-8");
5151
m_hasDecodingError = false;
5252
m_bReadOnly = false;
53+
m_bBinary = false;
5354
m_hasUtf8Bom = false;
5455
m_lineTerminatorMode = NativeLineTerminator;
5556
}
@@ -67,6 +68,11 @@ bool LiteEditorFile::isReadOnly() const
6768
return m_bReadOnly;
6869
}
6970

71+
bool LiteEditorFile::isBinary() const
72+
{
73+
return m_bBinary;
74+
}
75+
7076
bool LiteEditorFile::saveText(const QString &fileName, const QString &text)
7177
{
7278
QFile file(fileName);
@@ -140,82 +146,81 @@ bool LiteEditorFile::loadFileHelper(const QString &fileName, const QString &mime
140146

141147
QByteArray buf = file.readAll();
142148
m_hasDecodingError = false;
149+
m_bBinary = false;
143150

144151
if (HasBinaryData(buf,32)) {
145-
m_liteApp->appendLog("LiteEditor","Binary file not open in the text editor! "+fileName,true);
146-
m_hasDecodingError = true;
147-
//outText = "error load binary file!!!";
148-
return false;
149-
}
150-
151-
if (bCheckCodec) {
152-
m_codec = QTextCodec::codecForName("UTF-8");
153-
m_hasUtf8Bom = false;
154-
155-
if (mimeType == "text/html" || mimeType == "text/xml") {
156-
m_codec = QTextCodec::codecForHtml(buf,QTextCodec::codecForName("utf-8"));
157-
} else {
158-
LiteApi::IMimeType *im = m_liteApp->mimeTypeManager()->findMimeType(mimeType);
159-
if (im) {
160-
QString codecName = im->codec();
161-
if (!codecName.isEmpty()) {
162-
m_codec = QTextCodec::codecForName(codecName.toLatin1());
163-
}
164-
}
165-
int bytesRead = buf.size();
166-
QTextCodec *codec = m_codec;
167-
// code taken from qtextstream
168-
if (bytesRead >= 4 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe && uchar(buf[2]) == 0 && uchar(buf[3]) == 0)
169-
|| (uchar(buf[0]) == 0 && uchar(buf[1]) == 0 && uchar(buf[2]) == 0xfe && uchar(buf[3]) == 0xff))) {
170-
codec = QTextCodec::codecForName("UTF-32");
171-
} else if (bytesRead >= 2 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe)
172-
|| (uchar(buf[0]) == 0xfe && uchar(buf[1]) == 0xff))) {
173-
codec = QTextCodec::codecForName("UTF-16");
174-
} else if (bytesRead >= 3 && uchar(buf[0]) == 0xef && uchar(buf[1]) == 0xbb && uchar(buf[2])== 0xbf) {
175-
codec = QTextCodec::codecForName("UTF-8");
176-
buf.remove(0,3);
177-
m_hasUtf8Bom = true;
178-
} else if (!codec){
179-
codec = QTextCodec::codecForLocale();
180-
}
181-
// end code taken from qtextstream
182-
m_codec = codec;
183-
}
184-
}
185-
186-
QTextCodec::ConverterState state;
187-
outText = m_codec->toUnicode(buf,buf.size(),&state);
188-
if (state.invalidChars > 0 || state.remainingChars > 0) {
189-
m_hasDecodingError = true;
190-
}
191-
if (m_hasDecodingError && bCheckCodec) {
152+
m_liteApp->appendLog("LiteEditor","Open binary file in the text editor! "+fileName,true);
153+
m_bBinary = true;
192154
QByteArray testName = m_libucd.parse(buf);
193155
if (!testName.isEmpty()) {
194156
QTextCodec *c = QTextCodec::codecForName(testName);
195157
if (c && (c->mibEnum() != m_codec->mibEnum()) ) {
196-
QTextCodec::ConverterState testState;
197-
QString testText = c->toUnicode(buf,buf.size(),&testState);
198-
if (testState.invalidChars == 0 && testState.remainingChars == 0) {
199-
m_hasDecodingError = false;
200-
m_codec = c;
201-
outText = testText;
158+
m_codec = c;
159+
}
160+
}
161+
QTextCodec::ConverterState state;
162+
outText = m_codec->toUnicode(buf,buf.size(),&state);
163+
if (state.invalidChars > 0 || state.remainingChars > 0) {
164+
m_hasDecodingError = true;
165+
}
166+
} else {
167+
if (bCheckCodec) {
168+
m_codec = QTextCodec::codecForName("UTF-8");
169+
m_hasUtf8Bom = false;
170+
171+
if (mimeType == "text/html" || mimeType == "text/xml") {
172+
m_codec = QTextCodec::codecForHtml(buf,QTextCodec::codecForName("utf-8"));
173+
} else {
174+
LiteApi::IMimeType *im = m_liteApp->mimeTypeManager()->findMimeType(mimeType);
175+
if (im) {
176+
QString codecName = im->codec();
177+
if (!codecName.isEmpty()) {
178+
m_codec = QTextCodec::codecForName(codecName.toLatin1());
179+
}
180+
}
181+
int bytesRead = buf.size();
182+
QTextCodec *codec = m_codec;
183+
// code taken from qtextstream
184+
if (bytesRead >= 4 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe && uchar(buf[2]) == 0 && uchar(buf[3]) == 0)
185+
|| (uchar(buf[0]) == 0 && uchar(buf[1]) == 0 && uchar(buf[2]) == 0xfe && uchar(buf[3]) == 0xff))) {
186+
codec = QTextCodec::codecForName("UTF-32");
187+
} else if (bytesRead >= 2 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe)
188+
|| (uchar(buf[0]) == 0xfe && uchar(buf[1]) == 0xff))) {
189+
codec = QTextCodec::codecForName("UTF-16");
190+
} else if (bytesRead >= 3 && uchar(buf[0]) == 0xef && uchar(buf[1]) == 0xbb && uchar(buf[2])== 0xbf) {
191+
codec = QTextCodec::codecForName("UTF-8");
192+
buf.remove(0,3);
193+
m_hasUtf8Bom = true;
194+
} else if (!codec){
195+
codec = QTextCodec::codecForLocale();
196+
}
197+
// end code taken from qtextstream
198+
m_codec = codec;
199+
}
200+
}
201+
202+
QTextCodec::ConverterState state;
203+
outText = m_codec->toUnicode(buf,buf.size(),&state);
204+
if (state.invalidChars > 0 || state.remainingChars > 0) {
205+
m_hasDecodingError = true;
206+
}
207+
208+
if (m_hasDecodingError && bCheckCodec) {
209+
QByteArray testName = m_libucd.parse(buf);
210+
if (!testName.isEmpty()) {
211+
QTextCodec *c = QTextCodec::codecForName(testName);
212+
if (c && (c->mibEnum() != m_codec->mibEnum()) ) {
213+
QTextCodec::ConverterState testState;
214+
QString testText = c->toUnicode(buf,buf.size(),&testState);
215+
if (testState.invalidChars == 0 && testState.remainingChars == 0) {
216+
m_hasDecodingError = false;
217+
m_codec = c;
218+
outText = testText;
219+
}
202220
}
203221
}
204222
}
205223
}
206-
/*
207-
QByteArray verifyBuf = m_codec->fromUnicode(text); // slow
208-
// the minSize trick lets us ignore unicode headers
209-
int minSize = qMin(verifyBuf.size(), buf.size());
210-
m_hasDecodingError = (minSize < buf.size()- 4
211-
|| memcmp(verifyBuf.constData() + verifyBuf.size() - minSize,
212-
buf.constData() + buf.size() - minSize, minSize));
213-
*/
214-
/*
215-
if (text.length()*2+4 < buf.length()) {
216-
m_hasDecodingError = true;
217-
}
218-
*/
219224

220225
int lf = outText.indexOf('\n');
221226
if (lf < 0) {

liteidex/src/plugins/liteeditor/liteeditorfile.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LiteEditorFile : public LiteApi::IFile
3838
virtual bool reloadTextByCodec(const QString &codecName, QString &outText);
3939
virtual bool saveText(const QString &filePath, const QString &text);
4040
virtual bool isReadOnly() const;
41+
virtual bool isBinary() const;
4142
virtual QString filePath() const;
4243
virtual void setMimeType(const QString &mimeType);
4344
virtual QString mimeType() const;
@@ -64,6 +65,7 @@ class LiteEditorFile : public LiteApi::IFile
6465
protected:
6566
bool m_hasDecodingError;
6667
bool m_bReadOnly;
68+
bool m_bBinary;
6769
LiteApi::IApplication *m_liteApp;
6870
QString m_fileName;
6971
QString m_mimeType;

liteidex/src/utils/modelproject/modelfileimpl.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ bool ModelFileImpl::isReadOnly() const
7272
return m_bReadOnly;
7373
}
7474

75+
bool ModelFileImpl::isBinary() const
76+
{
77+
return false;
78+
}
79+
7580
bool ModelFileImpl::saveText(const QString &/*fileName*/, const QString &/*text*/)
7681
{
7782
return false;

liteidex/src/utils/modelproject/modelfileimpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ModelFileImpl : public LiteApi::IFile
4343
virtual bool reloadText(QString &outText);
4444
virtual bool saveText(const QString &filePath, const QString &text);
4545
virtual bool isReadOnly() const;
46+
virtual bool isBinary() const;
4647
virtual QString filePath() const;
4748
virtual QString mimeType() const;
4849
public:

0 commit comments

Comments
 (0)