1 Star 0 Fork 0

陈钰坤/Idea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 3.88 KB
一键复制 编辑 原始数据 按行查看 历史
陈钰坤 提交于 2023-07-17 11:03 . fixbug:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QKeyEvent>
#include <QFile>
#include <QDateTime>
#include <QTime>
#include <QDebug>
#include <QDir>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textEdit->installEventFilter(this);
showDays();
QDateTime time = QDateTime::currentDateTime();
showIdeas(time.toString("yyyy-MM-dd"));
}
MainWindow::~MainWindow()
{
delete ui;
}
// if key == 2 mean ctrl + enter
int key = 0;
void MainWindow::keyPressEvent(QKeyEvent* event) {
if(event->key() == Qt::Key_Control) {
key++;
}
}
void MainWindow::keyReleaseEvent(QKeyEvent* event) {
if(event->key() == Qt::Key_Control) {
key--;
}
}
bool MainWindow::showDays() {
ui->comboBox->clear();
QDir dir;
QString dir_str = "./data/";
if(!dir.exists(dir_str)) {
bool res = dir.mkpath(dir_str);
}
dir.setPath(dir_str);
QStringList dirs = dir.entryList(QDir::Dirs, QDir::Name);
for(auto dir: dirs) {
if(dir.startsWith(".") || dirs.startsWith("..")) continue;
ui->comboBox->addItem(dir);
QDateTime time = QDateTime::currentDateTime();
if(dir == time.toString("yyyy-MM-dd")) {
ui->comboBox->setCurrentText(dir);
}
}
}
bool MainWindow::submitIdea() {
QString idea = ui->textEdit->toPlainText();
QFile f;
QDateTime time = QDateTime::currentDateTime();
QDir dir;
QString dir_str = "./data/" + time.toString("yyyy-MM-dd");
if(!dir.exists(dir_str)) {
bool res = dir.mkpath(dir_str);
if(res) {
// new day
showDays();
}
}
QString filename = dir_str + "/" + time.toString("yyyy-MM-dd-hh-mm-ss") + ".txt";
f.setFileName(filename);
if(f.open(QIODevice::WriteOnly)) {
QTextStream in(&f);
in.setEncoding(QStringConverter::Utf8);
in << idea;
f.close();
} else {
qDebug() << f.errorString();
}
ui->textEdit->clear();
QString dirname = time.toString("yyyy-MM-dd");
showIdeas(dirname);
}
bool MainWindow::showIdeas(QString dirname) {
// update the list by the name of dirname
ui->listWidget->clear();
QString idea;
QDateTime time = QDateTime::currentDateTime();
QDir dir;
QString dir_str = "./data/" + dirname;
if(!dir.exists(dir_str)) {
bool res = dir.mkpath(dir_str);
}
QDir new_dir(dir_str);
QStringList files = new_dir.entryList(QDir::Files|QDir::Readable, QDir::Name);
for(auto file: files) {
qDebug() << file;
}
QStringList ideaList;
QFile f;
for(auto file : files) {
f.setFileName(dir_str + "/" + file);
if(f.open(QIODevice::ReadOnly | QIODevice::WriteOnly)) {
QTextStream out(&f);
out.setEncoding(QStringConverter::Utf8);
idea = out.readAll();
qDebug() << idea;
ideaList.push_back(file + "\n" + idea);
f.close();
} else {
// open error
}
}
for(auto idea : ideaList) {
ui->listWidget->addItem(idea);
ui->listWidget->scrollToBottom();
}
}
bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
if(target == ui->textEdit) {
if(event->type() == QEvent::KeyPress) {
QKeyEvent *k = static_cast<QKeyEvent *>(event);
if(k->key() == Qt::Key_Return && key == 1) {
submitIdea();
return true;
}
}
}
return QWidget::eventFilter(target, event);
}
void MainWindow::on_textEdit_textChanged()
{
}
void MainWindow::on_comboBox_activated(int index)
{
// no need to update the comboBox self because when the first submit of the new days generate
// comboBox will update and select the new day.
showIdeas(ui->comboBox->currentText());
qDebug() << ui->comboBox->currentText();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chen_yu_kong/idea.git
[email protected]:chen_yu_kong/idea.git
chen_yu_kong
idea
Idea
master

搜索帮助