- print support

This commit is contained in:
royqh1979@gmail.com 2021-10-07 07:52:20 +08:00
parent ec458a880a
commit b0442258db
12 changed files with 111 additions and 35 deletions

View File

@ -26,6 +26,7 @@ Version 0.6.0
- implement: config shortcuts - implement: config shortcuts
- implement: handle windows logout message - implement: handle windows logout message
- fix: editor's inproject property not correctly setted (and may cause devcpp to crash when close project) - fix: editor's inproject property not correctly setted (and may cause devcpp to crash when close project)
- implement: print
Version 0.5.0 Version 0.5.0
- enhancement: support C++ using type alias; - enhancement: support C++ using type alias;

View File

@ -1,4 +1,4 @@
QT += core gui QT += core gui printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

View File

@ -24,6 +24,9 @@
#include <QToolTip> #include <QToolTip>
#include <QApplication> #include <QApplication>
#include <QInputDialog> #include <QInputDialog>
#include <QPrinter>
#include <QPrintDialog>
#include <QTextDocument>
#include "iconsmanager.h" #include "iconsmanager.h"
#include "debugger.h" #include "debugger.h"
#include "editorlist.h" #include "editorlist.h"
@ -1997,6 +2000,44 @@ void Editor::insertCodeSnippet(const QString &code)
} }
} }
void Editor::print()
{
QPrinter printer;
QPrintDialog dialog(&printer, this);
dialog.setWindowTitle(tr("Print Document"));
// if (editor->selAvail())
// dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection);
if (dialog.exec() != QDialog::Accepted) {
return;
}
QTextDocument doc;
// if (editor->selAvail()) {
// doc.setPlainText(editor->selText());
// } else {
QStringList lst = contents();
for (int i=0;i<lst.length();i++) {
int columns = 0;
QString line = lst[i];
QString newLine;
for (QChar ch:line) {
if (ch=='\t') {
int charCol = tabWidth() - (columns % tabWidth());
newLine += QString(charCol,' ');
columns += charCol;
} else {
newLine+=ch;
columns+=charColumns(ch);
}
}
lst[i]=newLine;
}
doc.setDefaultFont(font());
doc.setPlainText(lst.join(lineBreak()));
doc.print(&printer);
}
void Editor::showCompletion(bool autoComplete) void Editor::showCompletion(bool autoComplete)
{ {
if (!pSettings->codeCompletion().enabled()) if (!pSettings->codeCompletion().enabled())
@ -3136,7 +3177,7 @@ void Editor::reformat()
return; return;
//we must remove all breakpoints and syntax issues //we must remove all breakpoints and syntax issues
onLinesDeleted(1,lines()->count()); onLinesDeleted(1,lines()->count());
QByteArray content = lines()->text().toUtf8(); QByteArray content = text().toUtf8();
QStringList args = pSettings->codeFormatter().getArguments(); QStringList args = pSettings->codeFormatter().getArguments();
QByteArray newContent = runAndGetOutput("astyle.exe", QByteArray newContent = runAndGetOutput("astyle.exe",
pSettings->dirs().app(), pSettings->dirs().app(),

View File

@ -158,6 +158,7 @@ public:
void reparseTodo(); void reparseTodo();
void insertString(const QString& value, bool moveCursor); void insertString(const QString& value, bool moveCursor);
void insertCodeSnippet(const QString& code); void insertCodeSnippet(const QString& code);
void print();
const PCppParser &parser(); const PCppParser &parser();

View File

@ -234,6 +234,7 @@ void MainWindow::updateEditorActions()
ui->actionRedo->setEnabled(false); ui->actionRedo->setEnabled(false);
ui->actionSave->setEnabled(false); ui->actionSave->setEnabled(false);
ui->actionSaveAs->setEnabled(false); ui->actionSaveAs->setEnabled(false);
ui->actionPrint->setEnabled(false);
ui->actionSelectAll->setEnabled(false); ui->actionSelectAll->setEnabled(false);
ui->actionToggleComment->setEnabled(false); ui->actionToggleComment->setEnabled(false);
ui->actionUnIndent->setEnabled(false); ui->actionUnIndent->setEnabled(false);
@ -266,6 +267,7 @@ void MainWindow::updateEditorActions()
ui->actionUndo->setEnabled(e->canUndo()); ui->actionUndo->setEnabled(e->canUndo());
ui->actionSave->setEnabled(e->modified()); ui->actionSave->setEnabled(e->modified());
ui->actionSaveAs->setEnabled(true); ui->actionSaveAs->setEnabled(true);
ui->actionPrint->setEnabled(true);
ui->actionSelectAll->setEnabled(e->lines()->count()>0); ui->actionSelectAll->setEnabled(e->lines()->count()>0);
ui->actionToggleComment->setEnabled(!e->readOnly() && e->lines()->count()>0); ui->actionToggleComment->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionUnIndent->setEnabled(!e->readOnly() && e->lines()->count()>0); ui->actionUnIndent->setEnabled(!e->readOnly() && e->lines()->count()>0);
@ -891,10 +893,10 @@ void MainWindow::checkSyntaxInBack(Editor *e)
ui->tableIssues->clearIssues(); ui->tableIssues->clearIssues();
CompileTarget target =getCompileTarget(); CompileTarget target =getCompileTarget();
if (target ==CompileTarget::Project) { if (target ==CompileTarget::Project) {
mCompilerManager->checkSyntax(e->filename(),e->lines()->text(), mCompilerManager->checkSyntax(e->filename(),e->text(),
e->fileEncoding() == ENCODING_ASCII, mProject); e->fileEncoding() == ENCODING_ASCII, mProject);
} else { } else {
mCompilerManager->checkSyntax(e->filename(),e->lines()->text(), mCompilerManager->checkSyntax(e->filename(),e->text(),
e->fileEncoding() == ENCODING_ASCII, nullptr); e->fileEncoding() == ENCODING_ASCII, nullptr);
} }
// if not PrepareForCompile(cttStdin,True) then begin // if not PrepareForCompile(cttStdin,True) then begin
@ -4108,3 +4110,12 @@ void MainWindow::on_btnCancelReplace_clicked()
{ {
showSearchReplacePanel(false); showSearchReplacePanel(false);
} }
void MainWindow::on_actionPrint_triggered()
{
Editor * editor = mEditorList->getEditor();
if (!editor)
return;
editor->print();
}

View File

@ -383,6 +383,8 @@ private slots:
void on_btnCancelReplace_clicked(); void on_btnCancelReplace_clicked();
void on_actionPrint_triggered();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
EditorList *mEditorList; EditorList *mEditorList;

View File

@ -854,6 +854,8 @@
<addaction name="actionClose_Project"/> <addaction name="actionClose_Project"/>
<addaction name="actionClose_All"/> <addaction name="actionClose_All"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionPrint"/>
<addaction name="separator"/>
<addaction name="actionExit"/> <addaction name="actionExit"/>
</widget> </widget>
<widget class="QMenu" name="menuTools"> <widget class="QMenu" name="menuTools">
@ -1808,6 +1810,18 @@
<string>Shift+F6</string> <string>Shift+F6</string>
</property> </property>
</action> </action>
<action name="actionPrint">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/images/newlook24/055-package.png</normaloff>:/icons/images/newlook24/055-package.png</iconset>
</property>
<property name="text">
<string>Print...</string>
</property>
<property name="shortcut">
<string>Ctrl+P</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -538,6 +538,12 @@ QStringList SynEdit::contents()
return lines()->contents(); return lines()->contents();
} }
QString SynEdit::text()
{
QMutexLocker locker(&mMutex);
return lines()->text();
}
bool SynEdit::getPositionOfMouse(BufferCoord &aPos) bool SynEdit::getPositionOfMouse(BufferCoord &aPos)
{ {
QPoint point = QCursor::pos(); QPoint point = QCursor::pos();
@ -4147,7 +4153,7 @@ void SynEdit::setHighlighter(const PSynHighlighter &highlighter)
invalidate(); invalidate();
} }
PSynEditStringList SynEdit::lines() const const PSynEditStringList& SynEdit::lines() const
{ {
return mLines; return mLines;
} }

View File

@ -258,6 +258,7 @@ public:
virtual BufferCoord getMatchingBracketEx(BufferCoord APoint); virtual BufferCoord getMatchingBracketEx(BufferCoord APoint);
QStringList contents(); QStringList contents();
QString text();
bool getPositionOfMouse(BufferCoord& aPos); bool getPositionOfMouse(BufferCoord& aPos);
bool getLineOfMouse(int& line); bool getLineOfMouse(int& line);
@ -309,7 +310,7 @@ public:
QString lineText() const; QString lineText() const;
void setLineText(const QString s); void setLineText(const QString s);
PSynEditStringList lines() const; const PSynEditStringList& lines() const;
bool empty(); bool empty();
SynSelectionMode selectionMode() const; SynSelectionMode selectionMode() const;

View File

@ -25,7 +25,7 @@ static void ListIndexOutOfBounds(int index) {
int SynEditStringList::parenthesisLevels(int Index) int SynEditStringList::parenthesisLevels(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.parenthesisLevel; return mList[Index]->fRange.parenthesisLevel;
@ -33,7 +33,7 @@ int SynEditStringList::parenthesisLevels(int Index)
return 0; return 0;
} }
int SynEditStringList::bracketLevels(int Index) int SynEditStringList::bracketLevels(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.bracketLevel; return mList[Index]->fRange.bracketLevel;
@ -41,7 +41,7 @@ int SynEditStringList::bracketLevels(int Index)
return 0; return 0;
} }
int SynEditStringList::braceLevels(int Index) int SynEditStringList::braceLevels(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.braceLevel; return mList[Index]->fRange.braceLevel;
@ -71,7 +71,7 @@ int SynEditStringList::lineColumns(int Index)
return 0; return 0;
} }
int SynEditStringList::leftBraces(int Index) int SynEditStringList::leftBraces(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fLeftBraces; return mList[Index]->fLeftBraces;
@ -79,7 +79,7 @@ int SynEditStringList::leftBraces(int Index)
return 0; return 0;
} }
int SynEditStringList::rightBraces(int Index) int SynEditStringList::rightBraces(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRightBraces; return mList[Index]->fRightBraces;
@ -87,8 +87,7 @@ int SynEditStringList::rightBraces(int Index)
return 0; return 0;
} }
int SynEditStringList::lengthOfLongestLine() int SynEditStringList::lengthOfLongestLine() {
{
if (mIndexOfLongestLine < 0) { if (mIndexOfLongestLine < 0) {
int MaxLen = -1; int MaxLen = -1;
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
@ -108,7 +107,7 @@ int SynEditStringList::lengthOfLongestLine()
return 0; return 0;
} }
QString SynEditStringList::lineBreak() QString SynEditStringList::lineBreak() const
{ {
switch(mFileEndingType) { switch(mFileEndingType) {
case FileEndingType::Linux: case FileEndingType::Linux:
@ -121,7 +120,7 @@ QString SynEditStringList::lineBreak()
return "\n"; return "\n";
} }
const SynRangeState& SynEditStringList::ranges(int Index) const SynRangeState& SynEditStringList::ranges(int Index) const
{ {
if (Index>=0 && Index < mList.size()) { if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange; return mList[Index]->fRange;
@ -178,7 +177,7 @@ void SynEditStringList::setRange(int Index, const SynRangeState& ARange, int ALe
endUpdate(); endUpdate();
} }
QString SynEditStringList::getString(int Index) QString SynEditStringList::getString(int Index) const
{ {
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
return QString(); return QString();
@ -186,12 +185,12 @@ QString SynEditStringList::getString(int Index)
return mList[Index]->fString; return mList[Index]->fString;
} }
int SynEditStringList::count() int SynEditStringList::count() const
{ {
return mList.count(); return mList.count();
} }
void *SynEditStringList::getObject(int Index) void *SynEditStringList::getObject(int Index) const
{ {
if (Index<0 || Index>=mList.count()) { if (Index<0 || Index>=mList.count()) {
return nullptr; return nullptr;
@ -199,7 +198,7 @@ void *SynEditStringList::getObject(int Index)
return mList[Index]->fObject; return mList[Index]->fObject;
} }
QString SynEditStringList::text() QString SynEditStringList::text() const
{ {
return getTextStr(); return getTextStr();
} }
@ -227,7 +226,7 @@ void SynEditStringList::setContents(const QStringList &text)
} }
} }
QStringList SynEditStringList::contents() QStringList SynEditStringList::contents() const
{ {
QStringList Result; QStringList Result;
SynEditStringRecList list = mList; SynEditStringRecList list = mList;
@ -371,7 +370,7 @@ void SynEditStringList::deleteAt(int Index)
endUpdate(); endUpdate();
} }
QString SynEditStringList::getTextStr() QString SynEditStringList::getTextStr() const
{ {
QString result; QString result;
for (int i=0;i<mList.count()-1;i++) { for (int i=0;i<mList.count()-1;i++) {

View File

@ -50,23 +50,23 @@ class SynEditStringList : public QObject
public: public:
explicit SynEditStringList(SynEdit* pEdit,QObject* parent=nullptr); explicit SynEditStringList(SynEdit* pEdit,QObject* parent=nullptr);
int parenthesisLevels(int Index); int parenthesisLevels(int Index) const;
int bracketLevels(int Index); int bracketLevels(int Index) const;
int braceLevels(int Index); int braceLevels(int Index) const;
int lineColumns(int Index); int lineColumns(int Index);
int leftBraces(int Index); int leftBraces(int Index) const;
int rightBraces(int Index); int rightBraces(int Index) const;
int lengthOfLongestLine(); int lengthOfLongestLine();
QString lineBreak(); QString lineBreak() const;
const SynRangeState& ranges(int Index); const SynRangeState& ranges(int Index) const;
void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces); void setRange(int Index, const SynRangeState& ARange, int leftBraces, int rightBraces);
QString getString(int Index); QString getString(int Index) const;
int count(); int count() const ;
void* getObject(int Index); void* getObject(int Index) const;
QString text(); QString text() const;
void setText(const QString& text); void setText(const QString& text);
void setContents(const QStringList& text); void setContents(const QStringList& text);
QStringList contents(); QStringList contents() const;
void putString(int Index, const QString& s); void putString(int Index, const QString& s);
void putObject(int Index, void * AObject); void putObject(int Index, void * AObject);
@ -111,7 +111,7 @@ signals:
void inserted(int index, int count); void inserted(int index, int count);
void putted(int index, int count); void putted(int index, int count);
protected: protected:
QString getTextStr(); QString getTextStr() const;
void SetUpdateState(bool Updating); void SetUpdateState(bool Updating);
void InsertItem(int Index, const QString& s); void InsertItem(int Index, const QString& s);
void addItem(const QString& s); void addItem(const QString& s);

View File

@ -21,7 +21,7 @@ EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& gro
if (!index.isValid()) if (!index.isValid())
return; return;
PCodeSnippet snippet = mModel.snippets()[index.row()]; PCodeSnippet snippet = mModel.snippets()[index.row()];
snippet->code = ui->editCode->lines()->text(); snippet->code = ui->editCode->text();
setSettingsChanged(); setSettingsChanged();
}); });
connect(ui->tblSnippets->selectionModel(), &QItemSelectionModel::currentChanged, connect(ui->tblSnippets->selectionModel(), &QItemSelectionModel::currentChanged,