mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
display selected file in a QTabWidget
Add VTabWidget and VEdit to display content of files. Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
b47c9f3368
commit
504e3323cf
@ -20,7 +20,9 @@ SOURCES += main.cpp\
|
|||||||
vnewdirdialog.cpp \
|
vnewdirdialog.cpp \
|
||||||
vconfigmanager.cpp \
|
vconfigmanager.cpp \
|
||||||
vfilelist.cpp \
|
vfilelist.cpp \
|
||||||
vnewfiledialog.cpp
|
vnewfiledialog.cpp \
|
||||||
|
vtabwidget.cpp \
|
||||||
|
vedit.cpp
|
||||||
|
|
||||||
HEADERS += vmainwindow.h \
|
HEADERS += vmainwindow.h \
|
||||||
vdirectorytree.h \
|
vdirectorytree.h \
|
||||||
@ -29,7 +31,9 @@ HEADERS += vmainwindow.h \
|
|||||||
vnewdirdialog.h \
|
vnewdirdialog.h \
|
||||||
vconfigmanager.h \
|
vconfigmanager.h \
|
||||||
vfilelist.h \
|
vfilelist.h \
|
||||||
vnewfiledialog.h
|
vnewfiledialog.h \
|
||||||
|
vtabwidget.h \
|
||||||
|
vedit.h
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
vnote.qrc
|
vnote.qrc
|
||||||
|
45
vedit.cpp
Normal file
45
vedit.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include <QtWidgets>
|
||||||
|
#include "vedit.h"
|
||||||
|
|
||||||
|
VEdit::VEdit(const QString &path, const QString &name,
|
||||||
|
QWidget *parent) : QTextEdit(parent), path(path), name(name)
|
||||||
|
{
|
||||||
|
fileText = readFile(QDir(path).filePath(name));
|
||||||
|
showTextReadMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::showTextReadMode()
|
||||||
|
{
|
||||||
|
setText(fileText);
|
||||||
|
setReadOnly(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString VEdit::readFile(const QString &filePath)
|
||||||
|
{
|
||||||
|
QFile file(filePath);
|
||||||
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
qWarning() << "error: fail to read file" << filePath;
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
QString fileText(file.readAll());
|
||||||
|
file.close();
|
||||||
|
return fileText;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VEdit::writeFile(const QString &filePath, const QString &text)
|
||||||
|
{
|
||||||
|
QFile file(filePath);
|
||||||
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
|
qWarning() << "error: fail to open file" << filePath << "to write to";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
QTextStream stream(&file);
|
||||||
|
stream << text;
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VEdit::requestClose()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
30
vedit.h
Normal file
30
vedit.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef VEDIT_H
|
||||||
|
#define VEDIT_H
|
||||||
|
|
||||||
|
#include <QTextEdit>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
class VEdit : public QTextEdit
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit VEdit(const QString &path, const QString &name,
|
||||||
|
QWidget *parent = 0);
|
||||||
|
bool requestClose();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString readFile(const QString &filePath);
|
||||||
|
bool writeFile(const QString &filePath, const QString &text);
|
||||||
|
void showTextReadMode();
|
||||||
|
|
||||||
|
QString path;
|
||||||
|
QString name;
|
||||||
|
QString fileText;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // VEDIT_H
|
@ -3,6 +3,7 @@
|
|||||||
#include "vdirectorytree.h"
|
#include "vdirectorytree.h"
|
||||||
#include "vnote.h"
|
#include "vnote.h"
|
||||||
#include "vfilelist.h"
|
#include "vfilelist.h"
|
||||||
|
#include "vtabwidget.h"
|
||||||
|
|
||||||
VMainWindow::VMainWindow(QWidget *parent)
|
VMainWindow::VMainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
@ -45,24 +46,15 @@ void VMainWindow::setupUI()
|
|||||||
fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
|
fileList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
|
||||||
|
|
||||||
// Editor tab widget
|
// Editor tab widget
|
||||||
editorTabWidget = new QTabWidget();
|
tabs = new VTabWidget(VNote::welcomePageUrl);
|
||||||
editorTabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
tabs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||||
editorTabWidget->setTabBarAutoHide(true);
|
tabs->setTabBarAutoHide(true);
|
||||||
QFile welcomeFile(":/resources/welcome.html");
|
|
||||||
QString welcomeText("Welcome to VNote!");
|
|
||||||
if (welcomeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
||||||
welcomeText = QString(welcomeFile.readAll());
|
|
||||||
welcomeFile.close();
|
|
||||||
}
|
|
||||||
QTextBrowser *welcomePage = new QTextBrowser();
|
|
||||||
welcomePage->setHtml(welcomeText);
|
|
||||||
editorTabWidget->addTab(welcomePage, tr("Welcome to VNote"));
|
|
||||||
|
|
||||||
// Main Splitter
|
// Main Splitter
|
||||||
mainSplitter = new QSplitter();
|
mainSplitter = new QSplitter();
|
||||||
mainSplitter->addWidget(nbContainer);
|
mainSplitter->addWidget(nbContainer);
|
||||||
mainSplitter->addWidget(fileList);
|
mainSplitter->addWidget(fileList);
|
||||||
mainSplitter->addWidget(editorTabWidget);
|
mainSplitter->addWidget(tabs);
|
||||||
mainSplitter->setStretchFactor(0, 1);
|
mainSplitter->setStretchFactor(0, 1);
|
||||||
mainSplitter->setStretchFactor(1, 1);
|
mainSplitter->setStretchFactor(1, 1);
|
||||||
mainSplitter->setStretchFactor(2, 10);
|
mainSplitter->setStretchFactor(2, 10);
|
||||||
@ -74,6 +66,8 @@ void VMainWindow::setupUI()
|
|||||||
SLOT(setTreePath(const QString&)));
|
SLOT(setTreePath(const QString&)));
|
||||||
connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
|
connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
|
||||||
fileList, &VFileList::setDirectory);
|
fileList, &VFileList::setDirectory);
|
||||||
|
connect(fileList, &VFileList::currentFileChanged,
|
||||||
|
tabs, &VTabWidget::openFile);
|
||||||
|
|
||||||
setCentralWidget(mainSplitter);
|
setCentralWidget(mainSplitter);
|
||||||
// Create and show the status bar
|
// Create and show the status bar
|
||||||
|
@ -11,6 +11,7 @@ class QListWidget;
|
|||||||
class QTabWidget;
|
class QTabWidget;
|
||||||
class VNote;
|
class VNote;
|
||||||
class VFileList;
|
class VFileList;
|
||||||
|
class VTabWidget;
|
||||||
|
|
||||||
class VMainWindow : public QMainWindow
|
class VMainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
@ -36,7 +37,7 @@ private:
|
|||||||
QComboBox *notebookComboBox;
|
QComboBox *notebookComboBox;
|
||||||
VDirectoryTree *directoryTree;
|
VDirectoryTree *directoryTree;
|
||||||
VFileList *fileList;
|
VFileList *fileList;
|
||||||
QTabWidget *editorTabWidget;
|
VTabWidget *tabs;
|
||||||
QSplitter *mainSplitter;
|
QSplitter *mainSplitter;
|
||||||
VNote *vnote;
|
VNote *vnote;
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
const QString VNote::orgName = QString("tamlok");
|
const QString VNote::orgName = QString("tamlok");
|
||||||
const QString VNote::appName = QString("VNote");
|
const QString VNote::appName = QString("VNote");
|
||||||
|
const QString VNote::welcomePageUrl = QString(":/resources/welcome.html");
|
||||||
|
|
||||||
VNote::VNote()
|
VNote::VNote()
|
||||||
: curNotebookIndex(0)
|
: curNotebookIndex(0)
|
||||||
|
1
vnote.h
1
vnote.h
@ -19,6 +19,7 @@ public:
|
|||||||
|
|
||||||
static const QString orgName;
|
static const QString orgName;
|
||||||
static const QString appName;
|
static const QString appName;
|
||||||
|
static const QString welcomePageUrl;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Write notebooks section of global config
|
// Write notebooks section of global config
|
||||||
|
94
vtabwidget.cpp
Normal file
94
vtabwidget.cpp
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include <QtWidgets>
|
||||||
|
#include <QtDebug>
|
||||||
|
#include "vtabwidget.h"
|
||||||
|
#include "vedit.h"
|
||||||
|
|
||||||
|
VTabWidget::VTabWidget(const QString &welcomePageUrl, QWidget *parent)
|
||||||
|
: QTabWidget(parent), welcomePageUrl(welcomePageUrl)
|
||||||
|
{
|
||||||
|
setTabsClosable(true);
|
||||||
|
connect(tabBar(), &QTabBar::tabCloseRequested,
|
||||||
|
this, &VTabWidget::handleTabCloseRequest);
|
||||||
|
|
||||||
|
openWelcomePage();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VTabWidget::openWelcomePage()
|
||||||
|
{
|
||||||
|
int idx = openFileInTab(welcomePageUrl, "");
|
||||||
|
setTabText(idx, "Welcome to VNote");
|
||||||
|
setTabToolTip(idx, "VNote");
|
||||||
|
}
|
||||||
|
|
||||||
|
int VTabWidget::insertTabWithData(int index, QWidget *page, const QString &label,
|
||||||
|
const QJsonObject &tabData)
|
||||||
|
{
|
||||||
|
int idx = insertTab(index, page, label);
|
||||||
|
QTabBar *tabs = tabBar();
|
||||||
|
tabs->setTabData(idx, tabData);
|
||||||
|
Q_ASSERT(tabs->tabText(idx) == label);
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
int VTabWidget::appendTabWithData(QWidget *page, const QString &label, const QJsonObject &tabData)
|
||||||
|
{
|
||||||
|
return insertTabWithData(count(), page, label, tabData);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VTabWidget::openFile(QJsonObject fileJson)
|
||||||
|
{
|
||||||
|
if (fileJson.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
qDebug() << "open file:" << fileJson;
|
||||||
|
|
||||||
|
QString path = fileJson["path"].toString();
|
||||||
|
QString name = fileJson["name"].toString();
|
||||||
|
|
||||||
|
// Find if it has been opened already
|
||||||
|
int idx = findTabByFile(path, name);
|
||||||
|
if (idx > -1) {
|
||||||
|
setCurrentIndex(idx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx = openFileInTab(path, name);
|
||||||
|
|
||||||
|
setCurrentIndex(idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
int VTabWidget::openFileInTab(const QString &path, const QString &name)
|
||||||
|
{
|
||||||
|
VEdit *edit = new VEdit(path, name);
|
||||||
|
QJsonObject tabJson;
|
||||||
|
tabJson["path"] = path;
|
||||||
|
tabJson["name"] = name;
|
||||||
|
int idx = appendTabWithData(edit, name, tabJson);
|
||||||
|
setTabToolTip(idx, path);
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
int VTabWidget::findTabByFile(const QString &path, const QString &name)
|
||||||
|
{
|
||||||
|
QTabBar *tabs = tabBar();
|
||||||
|
int nrTabs = tabs->count();
|
||||||
|
|
||||||
|
for (int i = 0; i < nrTabs; ++i) {
|
||||||
|
QJsonObject tabJson = tabs->tabData(i).toJsonObject();
|
||||||
|
if (tabJson["name"] == name && tabJson["path"] == path) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VTabWidget::handleTabCloseRequest(int index)
|
||||||
|
{
|
||||||
|
qDebug() << "request closing tab" << index;
|
||||||
|
VEdit *edit = dynamic_cast<VEdit *>(widget(index));
|
||||||
|
bool ok = edit->requestClose();
|
||||||
|
if (ok) {
|
||||||
|
removeTab(index);
|
||||||
|
delete edit;
|
||||||
|
}
|
||||||
|
}
|
31
vtabwidget.h
Normal file
31
vtabwidget.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef VTABWIDGET_H
|
||||||
|
#define VTABWIDGET_H
|
||||||
|
|
||||||
|
#include <QTabWidget>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
class VTabWidget : public QTabWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit VTabWidget(const QString &welcomePageUrl, QWidget *parent = 0);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void openFile(QJsonObject fileJson);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void handleTabCloseRequest(int index);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void openWelcomePage();
|
||||||
|
int insertTabWithData(int index, QWidget *page, const QString &label, const QJsonObject &tabData);
|
||||||
|
int appendTabWithData(QWidget *page, const QString &label, const QJsonObject &tabData);
|
||||||
|
int findTabByFile(const QString &path, const QString &name);
|
||||||
|
int openFileInTab(const QString &path, const QString &name);
|
||||||
|
QString welcomePageUrl;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // VTABWIDGET_H
|
Loading…
x
Reference in New Issue
Block a user