#include "mainwindow.h"
#include <QPrintDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QPrintEngine>
#include <QTextStream>
#include <QPrinter>
#include <QPrintPreviewDialog>
#include <QTabWidget>
#include <QTextDocumentWriter>
#include <QMessageBox>
#include <QFileDialog>
#include <QKeySequence>
#include <QMenuBar>
#include <QToolBar>
#include <QFont>
#include <QClipboard>
#include <QComboBox>
#include <QFontComboBox>
#include <QFontDatabase>
#include <QActionGroup>
#include <QTextCharFormat>
#include <QMimeData>
MainWindow::MainWindow(Config *config, QWidget *parent) :
QMainWindow(parent), config(config)
{
init();
setupFileMenu();
setupEditMenu();
setupFormatMenu();
setupWindowMenu();
setupHelpMenu();
currentChanged(-1);
currentChanged(0);
setupEditActions();
setCentralWidget(tabWidget);
restoreGeometry(config->mainWindowsGeometry);
restoreState(config->mainWindowState);
}
//初始化
void MainWindow::init()
{
menuBar = new QMenuBar(this);
topToolBar = new QToolBar(this);
topToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
bottomToolBar = new QToolBar(this);
bottomToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
setMenuBar(menuBar);
addToolBar(topToolBar);
addToolBarBreak(Qt::TopToolBarArea);
addToolBar(bottomToolBar);
comboFont = new QFontComboBox(bottomToolBar);
comboFont->setCurrentFont(QFont(config->fontFamily));
bottomToolBar->addWidget(comboFont);
comboStyle = new QComboBox(bottomToolBar);
comboStyle->addItem("Normal");
comboStyle->addItem("Bold");
comboStyle->addItem("Italic");
comboStyle->addItem("Bold Italic");
comboStyle->setCurrentIndex(comboStyle->findText(config->fontStyle));
bottomToolBar->addWidget(comboStyle);
comboSize = new QComboBox(bottomToolBar);
comboSize->setEditable(true);
QFontDatabase db;
foreach(int size, db.standardSizes())
comboSize->addItem(QString::number(size));
comboSize->setCurrentIndex(
comboSize->findText(tr("%1").arg(config->fontSize)));
bottomToolBar->addWidget(comboSize);
tabWidget = new QTabWidget(this);
tabWidget->setMovable(true);
tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
tabWidget->setTabsClosable(true);
connect(tabWidget,SIGNAL(currentChanged(int)),this,SLOT(currentChanged(int)));
connect(tabWidget,SIGNAL(tabCloseRequested(int)),this,SLOT(fileClose(int)));
searchDialog = new SearchDialog(config);
searchDialog->setVisible(false);
gotoLineDialog = new GoToLineDialog;
gotoLineDialog->setVisible(false);
perferenceDialog = new PerferenceDialog(config, this);
perferenceDialog->setVisible(false);
}
//关闭事件
void MainWindow::closeEvent(QCloseEvent *event)
{
for (int i = 0; i < tabWidget->count(); i++)
{
tabWidget->setCurrentIndex(i);
if (!maybeSave(i))
{
event->ignore();
return;
}
}
event->accept();
}
//文档发生改变
void MainWindow::modificationChanged(bool changed)
{
QString str = tabWidget->tabText(tabWidget->currentIndex());
if (str[str.length() - 1] == '*')
{
if (!changed)
str.resize(str.length() - 1);
}
else if (changed)
{
str += '*';
}
tabWidget->setTabText(tabWidget->currentIndex(), str);
refreshActions();
setupEditActions();
}
//文档选中状态发生改变
void MainWindow::selectionChanged()
{
upperCaseAct->setEnabled(EDITOR->textCursor().hasSelection());
lowerCaseAct->setEnabled(EDITOR->textCursor().hasSelection());
QTextCharFormat fmt=EDITOR->currentCharFormat();
currentCharFormatChanged(fmt);
}
//文档段落发生改变
void MainWindow::blockCountChanged(int blockCount)
{
gotoLineDialog->setMaxLineNumber(blockCount);
}
//Tab发生改变时触发的槽
void MainWindow::currentChanged(int index)
{
if (index == -1)
{
newNumber = 0;
if (config->showReadme)
showReadme();
else
newFile();
updateTextStyleActs(config->fontStyle);
return;
}
updateActions();
setWindowIcon(QIcon(tr(":images/notepad.png")));
setWindowTitle(tr("NotePad(%1)").arg(openedFiles.at(index)));
}
//创建新的Tab(用于打开文件)
void MainWindow::newTab(const QString& fileName, QFile& file)
{
openedFiles << fileName;
NotePad *notePad = new NotePad(config);
tabWidget->addTab(notePad, QFileInfo(fileName).fileName());
QByteArray data = file.readAll();
notePad->setPlainText(QString::fromLocal8Bit(data));
tabWidget->setCurrentWidget(notePad);
}
//显示readme文件
void MainWindow::showReadme()
{
QString readmeFile = QApplication::applicationDirPath();
readmeFile += readmeFile.endsWith('/') ? "readme.txt" : "/readme.txt";
if (!QFile::exists(readmeFile))
{
QFile file(readmeFile);
file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate);
QTextStream stream(&file);
stream
<< "/*===========================================================================\n"
<< "NotePad, editor for Common Use.\n"
<< "Copyright (C) 2012 Zouxingyu\n" << "\n"
<< "This program is free software: you can redistribute it and/or modify\n"
<< "it under the terms of the GNU General Public License as published by\n"
<< "the Free Software Foundation, either version 3 of the License, or\n"
<< "(at your option) any later version.\n" << "\n"
<< "This program is distributed in the hope that it will be useful,\n"
<< "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
<< "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
<< "GNU General Public License for more details.\n" << "\n"
<< "You should have received a copy of the GNU General Public License\n"
<< "along with this program. If not, see <http://www.gnu.org/licenses/>\n"
<< "===========================================================================*/";
file.close();
}
QFile rfile(readmeFile);
rfile.open(QFile::ReadOnly);
newTab(readmeFile, rfile);
}
//判断指定文件是否需要保存
bool MainWindow::maybeSave(int index)
{
NotePad *notePad = static_cast<NotePad*>(tabWidget->widget(index));
QString fileName = openedFiles.at(index);
if (!notePad->document()->isModified())
return true;
if (fileName.startsWith(QLatin1String(":/")))
return true;
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("NotePad"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save)
return fileSave(index);
else if (ret == QMessageBox::Cancel)
return false;
return true;
}
//更新config中最近打开的文件列表
void MainWindow::updateRecentFilesList()
{
int index = tabWidget->currentIndex();
QString fileName = openedFiles.at(index);
config->recentFiles.removeAll(fileName);
config->recentFiles.prepend(fileName);
if (config->recentFiles.size() > config->maxRecentFiles)
config->recentFiles.removeLast();
}
//更新action的状态(子函数)
void MainWindow::refreshActions()
{
saveAct->setEnabled(EDITOR->document()->isModified());
copyAct->setEnabled(EDITOR->textCursor().hasSelection());
cutAct->setEnabled(EDITOR->textCursor().hasSelection());
undoAct->setEnabled(EDITOR->document()->isUndoAvailable());
redoAct->setEnabled(EDITOR->document()->isRedoAvailable());
#ifndef QT_NO_CL
没有合适的资源?快使用搜索试试~ 我知道了~
Qt开发的文本编辑器
5星 · 超过95%的资源 需积分: 35 158 下载量 168 浏览量
2015-11-06
09:06:49
上传
评论 9
收藏 71KB GZ 举报
温馨提示
共60个文件
png:37个
cpp:7个
h:6个
开发环境:LinuxMint 17.1 QtCreator 3.3.1 Qt 5.4.1。实现了文本编辑(包括撤销、恢复、保存、打开文件、导出成pdf、另存为等等),Tab页式文档,行号显示,编辑内容转换为大写或小写,字体改变,编辑器设置,转到指定行。
资源推荐
资源详情
资源评论
收起资源包目录
notepad.tar.gz (60个子文件)
notepad
.project 808B
gotolinedialog.h 505B
notepad.qrc 2KB
searchdialog.h 649B
config.cpp 4KB
images
saveall.png 1KB
filenew.png 768B
notepad.png 850B
textleft.png 673B
editpaste.png 1KB
textunder.png 971B
next.png 1KB
editundo.png 2KB
exportpdf.png 1KB
uppercase.png 711B
fileclose.png 968B
zoomout.png 1KB
filesave.png 1KB
editcopy.png 1KB
printpreview.png 1015B
previous.png 1KB
closeall.png 545B
editcut.png 2KB
checked.png 1KB
fileopen.png 2KB
editredo.png 2KB
zoomin.png 1KB
editselectall.png 776B
textcenter.png 627B
lowercase.png 671B
editfind.png 1KB
fileprint.png 1KB
filesaveas.png 983B
textitalic.png 829B
fileexit.png 486B
textbold.png 1KB
helpaboutqt.png 1KB
logo32.png 1KB
textright.png 677B
textjustify.png 695B
textfont.png 843B
helpabout.png 1KB
perferencedialog.ui 14KB
searchdialog.cpp 2KB
notepad.pro.user 18KB
mainwindow.cpp 34KB
notepad.cpp 19KB
perferencedialog.h 621B
notepad.pro 506B
main.cpp 214B
.settings
language.settings.xml 2KB
config.h 2KB
.cproject 12KB
notepad.h 3KB
searchdialog.ui 7KB
mainwindow.h 7KB
gotolinedialog.ui 1KB
perferencedialog.cpp 3KB
gotolinedialog.cpp 645B
notepad.pro.user.2.5pre1 27KB
共 60 条
- 1
资源评论
- xinala11222017-10-26虽有部分bug,但已经很好了!谢谢共享资源,五星好评!
- wang135818251682017-08-21为什么打开报错啊啊
- qq_360994732018-06-27该教程很不错
zxywd
- 粉丝: 159
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功