Qt_Logger_Example/application/main_window.cpp

164 lines
5.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/****************************************************************************
** Copyright (c) 2025 Evgeny Teterin (nayk) <nayk@nxt.ru>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "main_window.h"
#include "./ui_main_window.h"
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QButtonGroup>
#include <QtCore/QDebug>
#include <QtCore/QLoggingCategory>
#include <QtGui/QDesktopServices>
#include <QtCore/QUrl>
#include <QtCore/QDir>
#include <QtWidgets/QMessageBox>
#include "logger.h"
//==============================================================================
Q_LOGGING_CATEGORY(mainWnd, "MainWindow")
//==============================================================================
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initialize();
}
//==============================================================================
MainWindow::~MainWindow()
{
delete ui;
}
//==============================================================================
void MainWindow::initialize()
{
setWindowTitle( QApplication::applicationName() );
connect(ui->radioButton1, &QRadioButton::toggled, this, &MainWindow::radioButtonToggled);
connect(ui->lineEdit1, &QLineEdit::editingFinished, this, &MainWindow::lineEditFinishEditing);
connect(ui->toolButton1, &QToolButton::clicked, this, &MainWindow::buttonClick);
connect(ui->pushButton1, &QPushButton::clicked, this, &MainWindow::buttonClick);
connect(ui->pushButton2, &QPushButton::clicked, this, &MainWindow::buttonClick);
connect(ui->pushButton2, &QPushButton::clicked, this, &MainWindow::buttonClick);
connect(ui->pushButtonOpen, &QPushButton::clicked, this, &MainWindow::buttonOpenClick);
}
//==============================================================================
void MainWindow::radioButtonToggled(bool checked)
{
#ifndef QT_NO_DEBUG
LOG_FUNCTION();
#endif
qCInfo(mainWnd) << "Переключение радиобаттона";
QAbstractButton *button = qobject_cast<QAbstractButton*>(sender());
if (button) {
qDebug() << "objectName:" << button->objectName()
<< "text: " << button->text()
<< "isChecked:" << checked;
}
else {
qCritical() << "Указатель не найден";
return;
}
}
//==============================================================================
void MainWindow::lineEditFinishEditing()
{
#ifndef QT_NO_DEBUG
LOG_FUNCTION();
#endif
qCInfo(mainWnd) << "Окончание редактирования текстового поля";
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender());
if (lineEdit) {
qDebug() << "objectName:" << lineEdit->objectName()
<< "text: " << lineEdit->text();
}
else {
qCritical() << "Указатель не найден";
return;
}
}
//==============================================================================
void MainWindow::buttonClick()
{
#ifndef QT_NO_DEBUG
LOG_FUNCTION();
#endif
qCInfo(mainWnd) << "Нажатие кнопки";
QAbstractButton *button = qobject_cast<QAbstractButton*>(sender());
if (button) {
qDebug() << "objectName:" << button->objectName()
<< "text: " << button->text();
}
else {
qCritical() << "Указатель не найден";
}
}
//==============================================================================
void MainWindow::buttonOpenClick()
{
#ifndef QT_NO_DEBUG
LOG_FUNCTION();
#endif
qCInfo(mainWnd) << "Нажатие кнопки открытия каталога логов";
QString path {Logger::logDirectory()};
QDir dir(path);
if (!dir.exists()) {
qWarning() << "Каталог не найден:" << path;
QMessageBox::critical(this, tr("Ошибка"), tr("Каталог не найден!"));
return;
}
bool success = QDesktopServices::openUrl(QUrl::fromLocalFile(path));
if (!success) {
qWarning() << "Не удалось открыть каталог:" << path;
QMessageBox::critical(this, tr("Ошибка"), tr("Не удалось открыть каталог!"));
}
}
//==============================================================================