#include "widget.h"
#include "ui_widget.h"
#include "inputdialog.h"
#include <QTimer>
#include <QFile>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
loadingData = true;
QScreen *screen = QGuiApplication::primaryScreen ();
QRect screenRect = screen->availableVirtualGeometry();
int dWidth = screenRect.width();
int dHeight = screenRect.height();//桌面大小
this->move(dWidth - this->width() - 30 ,30);
QPalette palette;
palette.setColor(QPalette::Window, QColor("cornsilk"));
this->setAutoFillBackground(true);
this->setPalette(palette);
ui->listWidget->setDragDropMode(QAbstractItemView::InternalMove);
connect(this->ui->listWidget,SIGNAL(itemSelectionChanged()),this,SLOT(on_item_selection_changed()));
connect(ui->listWidget->model(),&QAbstractItemModel::rowsMoved,this,&Widget::on_rows_Moved);
QTimer::singleShot(0, this, [=]() {
loadFromFile();
});
}
void Widget::on_rows_Moved()
{
saveToFile();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn_add_clicked()
{
InputDialog inputDialog;
int x = this->geometry().center().x() - inputDialog.width() / 2;
int y = this->geometry().center().y() - inputDialog.height() / 2;
inputDialog.move(x, y);
connect(&inputDialog,SIGNAL(inputAccepted(QString)),this,SLOT(onInputDialogAccepted(QString)));
inputDialog.exec();
saveToFile();
}
void Widget::on_btn_delete_clicked()
{
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
if (items.isEmpty()) {
return;
}
std::sort(items.begin(), items.end(), [](QListWidgetItem* a, QListWidgetItem* b){
return a->listWidget()->row(a) > b->listWidget()->row(b);
});
// 阻塞列表的信号并暂时禁止更新
ui->listWidget->blockSignals(true);
foreach(QListWidgetItem* item, items) {
delete item;
}
// 解除阻塞并更新列表
ui->listWidget->blockSignals(false);
ui->listWidget->update();
saveToFile();
}
void Widget::onInputDialogAccepted(QString text)
{
if(!text.isEmpty())
{
QListWidgetItem *item = new QListWidgetItem();
MyTextEdit *edit = new MyTextEdit();
edit->setReadOnly(true);
edit->setStyleSheet("background-color: rgb(228, 249, 224);");
edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
edit->setAlignment(Qt::AlignTop | Qt::AlignLeft);
edit->setWordWrapMode(QTextOption::WrapAnywhere);
edit->setText(text);
edit->setFixedWidth(ui->listWidget->viewport()->width());
int widgetHeight = edit->document()->size().height();
if (widgetHeight > 0)
{
edit->setFixedHeight(widgetHeight+5);
}
item->setSizeHint(QSize(ui->listWidget->viewport()->width(), edit->height()));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, edit);
item->setSizeHint(QSize(ui->listWidget->viewport()->width(), edit->height()));
// 等待文本编辑框的内容完全加载和显示
QApplication::processEvents();
// 调整文本编辑框的大小
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(item));
int widgetHeight = edit->document()->size().height() + 5;
edit->setFixedHeight(widgetHeight);
item->setSizeHint(QSize(ui->listWidget->viewport()->width(), edit->height()));
}
}
}
void Widget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(item));
edit->setFixedWidth(ui->listWidget->viewport()->width());
int widgetHeight = edit->document()->size().height()+5;
edit->setFixedHeight(widgetHeight);
item->setSizeHint(QSize(ui->listWidget->viewport()->width(), edit->height()));
}
}
void Widget::on_item_selection_changed()
{
if (loadingData == false)
{
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(item));
edit->setStyleSheet("background-color: rgb(228, 249, 224);");
}
QListWidgetItem* selectedItem = ui->listWidget->currentItem();
if (selectedItem && ui->listWidget->currentRow() >= 0)
{
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(selectedItem));
edit->setStyleSheet("background-color: forestgreen");
}
else
{
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(item));
edit->setStyleSheet("background-color: rgb(228, 249, 224);");
}
}
}
}
void Widget::saveToFile()
{
QString filePath = "D:/note.txt";
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::information(this, "警告", "文件写入失败");
return;
}
QTextStream out(&file);
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
if (item)
{
MyTextEdit *edit = qobject_cast<MyTextEdit*>(ui->listWidget->itemWidget(item));
if (edit)
{
QString text = edit->toPlainText();
out << text << "!@#$%^&*";
}
}
}
file.close();
}
void Widget::loadFromFile()
{
loadingData = true; // 开始加载数据标记为 true
QString filePath = "D:/note.txt";
QFile file(filePath);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
while (!in.atEnd())
{
QString content = in.readAll();
QStringList lines = content.split("!@#$%^&*", Qt::SkipEmptyParts);
for (int i = 0; i < lines.size(); ++i)
{
QString line = lines.at(i);
QListWidgetItem *item = new QListWidgetItem();
MyTextEdit *edit = new MyTextEdit();
edit->setReadOnly(true);
edit->setStyleSheet("background-color: rgb(228, 249, 224);");
edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
edit->setAlignment(Qt::AlignTop | Qt::AlignLeft);
edit->setWordWrapMode(QTextOption::WrapAnywhere);
edit->setText(line);
edit->setFixedWidth(ui->listWidget->viewport()->width());
item->setSizeHint(QSize(ui->listWidget->viewport()->width(), 0));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, edit);
}
}
file.close();
// 等待文本编辑框的内容完全加载和显示
QApplication::processEvents();
// 调整文本编辑框的大小
for (int i = 0; i < ui->listWidget->count(); ++i)
{
QListWidgetItem *item = ui->listWidget->item(i);
MyTextEdit *edit = qobject_cast<MyTextEdit *>(ui->listWidget->itemWidget(item));
int widgetHeight = edit->document()->size().height() + 5;
edit->setFixedHeight(widgetHeight);
item->setSizeHint(QSize(