Compare commits
No commits in common. "e64edc5c5a1e0afa7f6598f41caf1cf320471548" and "53b81f78c0362847ade6ebd9c02e1ccde5b152ab" have entirely different histories.
e64edc5c5a
...
53b81f78c0
5 changed files with 9 additions and 135 deletions
|
@ -1,7 +1,6 @@
|
||||||
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
|
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
|
||||||
XmlGui
|
XmlGui
|
||||||
TextWidgets
|
TextWidgets
|
||||||
KIO
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(TextEditor main.cc MainWindow.cc)
|
add_executable(TextEditor main.cc MainWindow.cc)
|
||||||
|
@ -11,7 +10,6 @@ target_link_libraries(TextEditor
|
||||||
KF5::I18n
|
KF5::I18n
|
||||||
KF5::XmlGui
|
KF5::XmlGui
|
||||||
KF5::TextWidgets
|
KF5::TextWidgets
|
||||||
KF5::KIOCore
|
|
||||||
)
|
)
|
||||||
|
|
||||||
install(TARGETS TextEditor ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
|
install(TARGETS TextEditor ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
#include "MainWindow.hh"
|
#include "MainWindow.hh"
|
||||||
#include <KActionCollection>
|
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
#include <KMessageBox>
|
|
||||||
#include <KStandardAction>
|
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
|
#include <KActionCollection>
|
||||||
|
#include <KStandardAction>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QFileDialog>
|
|
||||||
#include <QSaveFile>
|
|
||||||
#include <QTextStream>
|
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget* parent)
|
MainWindow::MainWindow(QWidget* parent)
|
||||||
: KXmlGuiWindow(parent)
|
: KXmlGuiWindow(parent)
|
||||||
, fileName(QString())
|
|
||||||
{
|
{
|
||||||
// setup the TextArea
|
// setup the TextArea
|
||||||
textArea = new KTextEdit();
|
textArea = new KTextEdit();
|
||||||
|
@ -21,112 +16,16 @@ MainWindow::MainWindow(QWidget* parent)
|
||||||
setupActions();
|
setupActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setupClearAction()
|
|
||||||
{
|
|
||||||
QAction* clearAction = new QAction(this);
|
|
||||||
|
|
||||||
clearAction->setText(i18n("&Clear"));
|
|
||||||
clearAction->setIcon(QIcon::fromTheme("document-new"));
|
|
||||||
|
|
||||||
actionCollection()->addAction("clear", clearAction);
|
|
||||||
actionCollection()->setDefaultShortcut(clearAction, Qt::CTRL + Qt::Key_W);
|
|
||||||
|
|
||||||
connect(clearAction, &QAction::triggered, textArea, &KTextEdit::clear);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::setupMeowAction()
|
|
||||||
{
|
|
||||||
QAction* meowAction = new QAction(this);
|
|
||||||
|
|
||||||
meowAction->setText(i18n("&Meow"));
|
|
||||||
meowAction->setIcon(QIcon::fromTheme("network-wireless-signal-excellent"));
|
|
||||||
|
|
||||||
actionCollection()->addAction("meow", meowAction);
|
|
||||||
actionCollection()->setDefaultShortcut(meowAction, Qt::CTRL + Qt::Key_M);
|
|
||||||
|
|
||||||
connect(meowAction, &QAction::triggered, this, &MainWindow::meow);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::setupActions()
|
void MainWindow::setupActions()
|
||||||
{
|
{
|
||||||
setupClearAction();
|
QAction* clearAction = new QAction(this);
|
||||||
setupMeowAction();
|
clearAction->setText(i18n("&Clear"));
|
||||||
|
clearAction->setIcon(QIcon::fromTheme("document-new"));
|
||||||
|
actionCollection()->addAction("clear", clearAction);
|
||||||
|
actionCollection()->setDefaultShortcut(clearAction, Qt::CTRL + Qt::Key_W);
|
||||||
|
connect(clearAction, &QAction::triggered, textArea, &KTextEdit::clear);
|
||||||
|
|
||||||
KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());
|
KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());
|
||||||
KStandardAction::open(this, &MainWindow::openFile, actionCollection());
|
|
||||||
KStandardAction::save(this, &MainWindow::saveFile, actionCollection());
|
|
||||||
KStandardAction::saveAs(this, &MainWindow::saveFileAs, actionCollection());
|
|
||||||
KStandardAction::openNew(this, &MainWindow::newFile, actionCollection());
|
|
||||||
|
|
||||||
setupGUI(Default, "TextEditorUI.rc");
|
setupGUI(Default, "TextEditorUI.rc");
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::saveFileToDisk(const QString& outputFileName)
|
|
||||||
{
|
|
||||||
if (outputFileName.isNull())
|
|
||||||
return;
|
|
||||||
|
|
||||||
QSaveFile file(outputFileName);
|
|
||||||
// open the file as write only
|
|
||||||
file.open(QIODevice::WriteOnly);
|
|
||||||
|
|
||||||
QByteArray outputByteArray;
|
|
||||||
outputByteArray.append(textArea->toPlainText().toUtf8());
|
|
||||||
|
|
||||||
file.write(outputByteArray);
|
|
||||||
file.commit();
|
|
||||||
|
|
||||||
fileName = outputFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::newFile()
|
|
||||||
{
|
|
||||||
fileName.clear();
|
|
||||||
textArea->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::openFile()
|
|
||||||
{
|
|
||||||
const QUrl fileNameFromDialog = QFileDialog::getOpenFileUrl(this, i18n("Open File"));
|
|
||||||
|
|
||||||
if (fileNameFromDialog.isEmpty())
|
|
||||||
return;
|
|
||||||
KIO::Job* job = KIO::storedGet(fileNameFromDialog);
|
|
||||||
fileName = fileNameFromDialog.toLocalFile();
|
|
||||||
|
|
||||||
connect(job, &KJob::result, this, &MainWindow::downloadFinished);
|
|
||||||
job->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::saveFile()
|
|
||||||
{
|
|
||||||
if (!fileName.isEmpty()) {
|
|
||||||
saveFileToDisk(fileName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
saveFileAs();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::saveFileAs()
|
|
||||||
{
|
|
||||||
saveFileToDisk(QFileDialog::getSaveFileName(this, i18n("Save File As")));
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::downloadFinished(KJob* job)
|
|
||||||
{
|
|
||||||
if (job->error()) {
|
|
||||||
KMessageBox::error(this, job->errorString());
|
|
||||||
fileName.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const KIO::StoredTransferJob* storedJob = qobject_cast<KIO::StoredTransferJob*>(job);
|
|
||||||
if (storedJob) {
|
|
||||||
textArea->setPlainText(QTextStream(storedJob->data(), QIODevice::ReadOnly).readAll());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::meow() {
|
|
||||||
KMessageBox::information(this, "Meow", "Meow");
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,31 +3,14 @@
|
||||||
|
|
||||||
#include <KTextEdit>
|
#include <KTextEdit>
|
||||||
#include <KXmlGuiWindow>
|
#include <KXmlGuiWindow>
|
||||||
#include <KIO/Job>
|
|
||||||
|
|
||||||
class MainWindow : public KXmlGuiWindow {
|
class MainWindow : public KXmlGuiWindow {
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
explicit MainWindow(QWidget* parent = nullptr);
|
explicit MainWindow(QWidget* parent = nullptr);
|
||||||
|
|
||||||
private:
|
|
||||||
void setupActions();
|
|
||||||
void saveFileToDisk(const QString& outputFileName);
|
|
||||||
void setupClearAction();
|
|
||||||
void setupMeowAction();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
|
||||||
void newFile();
|
|
||||||
void openFile();
|
|
||||||
void saveFile();
|
|
||||||
void saveFileAs();
|
|
||||||
void downloadFinished(KJob *job);
|
|
||||||
|
|
||||||
void meow();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
KTextEdit* textArea;
|
KTextEdit* textArea;
|
||||||
QString fileName;
|
void setupActions();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,14 +6,9 @@
|
||||||
<Menu name="file">
|
<Menu name="file">
|
||||||
<Action name="clear" />
|
<Action name="clear" />
|
||||||
</Menu>
|
</Menu>
|
||||||
<Menu>
|
|
||||||
<text>Another Meownu</text>
|
|
||||||
<Action name="meow" />
|
|
||||||
</Menu>
|
|
||||||
</MenuBar>
|
</MenuBar>
|
||||||
<ToolBar name="mainToolBar">
|
<ToolBar name="mainToolBar">
|
||||||
<text>Main Toolbar</text>
|
<text>Main Toolbar</text>
|
||||||
<Action name="clear" />
|
<Action name="clear" />
|
||||||
<Action name="meow" />
|
|
||||||
</ToolBar>
|
</ToolBar>
|
||||||
</gui>
|
</gui>
|
||||||
|
|
|
@ -23,7 +23,6 @@ int main(int argc, char* argv[])
|
||||||
// Copyright Statement
|
// Copyright Statement
|
||||||
i18n("2023"));
|
i18n("2023"));
|
||||||
|
|
||||||
// Set the AboutData to the aboutData variable
|
|
||||||
KAboutData::setApplicationData(aboutData);
|
KAboutData::setApplicationData(aboutData);
|
||||||
|
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
|
|
Loading…
Reference in a new issue