📄 notepad.cpp
字号:
#include "notepad.h"
#include <QtGui/QWidget>
#include <QtGui/QMessageBox>
#include <QtGui/QFileDialog>
notepad::notepad(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
notecontent = new noteContent;
setCentralWidget(notecontent);
createActions();
createMenus();
setWindowIcon(QIcon(":/images/icon.png"));
}
notepad::~notepad()
{
}
bool notepad::okToContinue()
{
if (notecontent->isModified())
{
int r = QMessageBox::warning(this, tr("notecontent"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::No
| QMessageBox::Cancel);
if (r == QMessageBox::Yes)
{
return save();
}
else if (r == QMessageBox::Cancel)
{
return false;
}
}
return true;
}
bool notepad::save()
{
if (curFile.isEmpty())
{
return saveAs();
}
else
{
return saveFile(curFile);
}
}
bool notepad::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save notecontent"), ".",
tr("notecontent files (*.nc)"));
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void notepad::open()
{
if (okToContinue()) {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Spreadsheet"), ".",
tr("Spreadsheet files (*.nc)"));
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool notepad::saveFile(const QString &fileName)
{
if (!notecontent->writeFile(fileName))
{
statusBar()->showMessage(tr("Saving canceled"), 2000);
return false;
}
//setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
bool notepad::loadFile(const QString &fileName)
{
if (!notecontent->readFile(fileName))
{
statusBar()->showMessage(tr("Loading canceled"), 2000);
return false;
}
//setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
return true;
}
void notepad::newFile()
{
if (okToContinue())
{
notecontent->clear();
//setCurrentFile("");
}
}
void notepad::createActions()
{
newAction = new QAction(tr("&New"),this);
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction,SIGNAL(triggered()),this,SLOT(newFile()));
openAction = new QAction(tr("&Open..."), this);
openAction->setIcon(QIcon(":/images/open.png"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing spreadsheet file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(tr("&Save"), this);
saveAction->setIcon(QIcon(":/images/save.png"));
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save the spreadsheet to disk"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setStatusTip(tr("Save the spreadsheet under a new "
"name"));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":/images/cut.png"));
cutAction->setShortcut(QKeySequence::Cut);
cutAction->setStatusTip(tr("Cut the current selection's contents "
"to the clipboard"));
connect(cutAction, SIGNAL(triggered()), notecontent, SLOT(cut()));
copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":/images/copy.png"));
copyAction->setShortcut(QKeySequence::Copy);
copyAction->setStatusTip(tr("Copy the current selection's contents "
"to the clipboard"));
connect(copyAction, SIGNAL(triggered()), notecontent, SLOT(copy()));
pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":/images/paste.png"));
pasteAction->setShortcut(QKeySequence::Paste);
pasteAction->setStatusTip(tr("Paste the clipboard's contents into "
"the current selection"));
connect(pasteAction, SIGNAL(triggered()),
notecontent, SLOT(paste()));
deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setShortcut(QKeySequence::Delete);
deleteAction->setStatusTip(tr("Delete the current selection's "
"contents"));
connect(deleteAction, SIGNAL(triggered()),
notecontent, SLOT(del()));
fontAction = new QAction(tr("&Font"), this);
fontAction->setStatusTip(tr("Set the font format"));
connect(fontAction, SIGNAL(triggered()),
this, SLOT(close()));
}
void notepad::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);
formatMenu = menuBar()->addMenu(tr("F&ormat"));
formatMenu->addAction(fontAction);
viewMenu = menuBar()->addMenu(tr("V&iew"));
helpMenu = menuBar()->addMenu(tr("H&elp"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -