File indexing completed on 2025-01-05 05:14:40

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "widgetbase.h"
0008 
0009 #include "gitmanager.h"
0010 
0011 #include "appwindow.h"
0012 #include <QDialog>
0013 #include <QHeaderView>
0014 #include <QPushButton>
0015 #include <QSplitter>
0016 #include <QTreeView>
0017 #include <QVBoxLayout>
0018 
0019 WidgetBase::WidgetBase(QWidget *parent)
0020     : QWidget(parent)
0021 {
0022     // TODO: remove this singelton call
0023     mGit = Git::Manager::instance();
0024     connect(mGit, &Git::Manager::pathChanged, this, &WidgetBase::git_pathChanged);
0025 }
0026 
0027 WidgetBase::WidgetBase(Git::Manager *git, AppWindow *parent)
0028     : QWidget(parent)
0029     , mGit(git)
0030     , mParent{parent}
0031 {
0032     // TODO: do we need this?
0033     if (!mGit)
0034         mGit = Git::Manager::instance();
0035     connect(mGit, &Git::Manager::pathChanged, this, &WidgetBase::git_pathChanged);
0036 }
0037 
0038 Git::Manager *WidgetBase::git() const
0039 {
0040     return mGit;
0041 }
0042 
0043 void WidgetBase::setGit(Git::Manager *newGit)
0044 {
0045     mGit = newGit;
0046 }
0047 
0048 void WidgetBase::reload()
0049 {
0050 }
0051 
0052 int WidgetBase::exec(QWidget *parent)
0053 {
0054     auto oldParent = this->parentWidget();
0055     QDialog d(parent);
0056     QVBoxLayout layout(&d);
0057     this->setParent(&d);
0058     layout.addWidget(this);
0059     //    layout.addWidget(new QPushButton(&d));
0060     int ret = d.exec();
0061     setParent(oldParent);
0062     return ret;
0063 }
0064 
0065 void WidgetBase::saveState(QSettings &settings) const
0066 {
0067     Q_UNUSED(settings)
0068 }
0069 
0070 void WidgetBase::restoreState(QSettings &settings)
0071 {
0072     Q_UNUSED(settings)
0073 }
0074 
0075 void WidgetBase::save(QSettings &settings, QSplitter *splitter) const
0076 {
0077     settings.setValue(stateName(splitter), splitter->saveState());
0078 }
0079 
0080 void WidgetBase::restore(QSettings &settings, QSplitter *splitter)
0081 {
0082     auto orientation = splitter->orientation();
0083     splitter->restoreState(settings.value(stateName(splitter)).toByteArray());
0084     splitter->setOrientation(orientation);
0085 }
0086 
0087 void WidgetBase::save(QSettings &settings, QTreeView *treeView) const
0088 {
0089     settings.setValue(stateName(treeView), treeView->header()->saveState());
0090 }
0091 
0092 void WidgetBase::restore(QSettings &settings, QTreeView *treeView)
0093 {
0094     treeView->header()->restoreState(settings.value(stateName(treeView)).toByteArray());
0095 }
0096 
0097 void WidgetBase::settingsUpdated()
0098 {
0099 }
0100 
0101 void WidgetBase::git_pathChanged()
0102 {
0103     reload();
0104 }
0105 
0106 QString WidgetBase::stateName(QWidget *w) const
0107 {
0108     return QStringLiteral("%1_%2_state").arg(metaObject()->className(), w->objectName());
0109 }
0110 
0111 #include "moc_widgetbase.cpp"