Skip to content

Commit

Permalink
chore: some code refactory
Browse files Browse the repository at this point in the history
- change CanSplit to a public funtion of TermWidget
- change MIN_WIDTH and MIN_HEIGHT of TermWidget to private
  • Loading branch information
hualet authored and ArchieMeng committed Sep 8, 2024
1 parent a72aac8 commit 084e086
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/main/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ inline void MainWindow::slotShortcutHorizonzalSplit()
// 判读数量是否允许分屏
if (Service::instance()->isCountEnable()) {
TermWidgetPage *page = currentPage();
if (page && CanSplit(page->currentTerminal(), Qt::Vertical)) {
if (page && page->currentTerminal()->canSplit(Qt::Vertical)) {
page->split(Qt::Horizontal);
return ;
}
Expand All @@ -1144,7 +1144,7 @@ inline void MainWindow::slotShortcutVerticalSplit()
// 判读数量是否允许分屏
if (Service::instance()->isCountEnable()) {
TermWidgetPage *page = currentPage();
if (page && CanSplit(page->currentTerminal(), Qt::Horizontal)) {
if (page && page->currentTerminal()->canSplit(Qt::Horizontal)) {
page->split(Qt::Vertical);
return ;
}
Expand Down
36 changes: 34 additions & 2 deletions src/views/termwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ void TermWidget::addMenuActions(const QPoint &pos)

QAction *action = 0;
action = m_menu->addAction(tr("Horizontal split"), this, &TermWidget::onHorizontalSplit);
action->setEnabled(CanSplit(this, Qt::Vertical));
action->setEnabled(canSplit(Qt::Vertical));
action = m_menu->addAction(tr("Vertical split"), this, &TermWidget::onVerticalSplit);
action->setEnabled(CanSplit(this, Qt::Horizontal));
action->setEnabled(canSplit(Qt::Horizontal));

/******** Modify by n014361 wangpeili 2020-02-21: 增加关闭窗口和关闭其它窗口菜单 ****************/
m_menu->addAction(QObject::tr("Close workspace"), this, &TermWidget::onCloseCurrWorkSpace);
Expand Down Expand Up @@ -1011,6 +1011,38 @@ bool TermWidget::isInRemoteServer()
return false;
}

bool TermWidget::canSplit(Qt::Orientation ori) {
qDebug() << "CanSplit:" << ori;
QSplitter *splitter = qobject_cast<QSplitter *>(this->parentWidget());
int minimumSize = ori == Qt::Horizontal ? TermWidget::MIN_WIDTH : TermWidget::MIN_HEIGHT;
if (splitter) {
if (splitter->orientation() == ori) {
QList<int> sizes = splitter->sizes();
// new term has same size portion as the current one.
sizes.append(sizes.at(splitter->indexOf(this)));

double sum = 0;
for (int i = 0; i < sizes.count(); i++) {
sum += sizes.at(i);
}

for(int i = 0; i < sizes.count(); i++) {
int totalSize = ori == Qt::Horizontal ? splitter->width() : splitter->height();
int actualSize = (totalSize) * (sizes.at(i) / sum);
if (actualSize < minimumSize)
return false;
}
} else {
int splitterSize = ori == Qt::Horizontal ? splitter->width() : splitter->height();
if (splitterSize / 2.0 < minimumSize)
return false;
}
}

return true;
}


void TermWidget::setTermOpacity(qreal opacity)
{
//这里再次判断一遍,因为刚启动时,还是需要判断一次当前是否开启了窗口特效
Expand Down
40 changes: 6 additions & 34 deletions src/views/termwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ class TermWidget : public QTermWidget
{
Q_OBJECT
public:
// 9:6
static const int MIN_WIDTH = 180;
static const int MIN_HEIGHT = 120;
bool canSplit(Qt::Orientation ori);

TermWidget(const TermProperties &properties, QWidget *parent = nullptr);

Check warning on line 70 in src/views/termwidget.h

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'TermWidget' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.

Check warning on line 70 in src/views/termwidget.h

View workflow job for this annotation

GitHub Actions / static-check / Call-CppCheck

Class 'TermWidget' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
~TermWidget();
Expand All @@ -83,6 +81,7 @@ class TermWidget : public QTermWidget
* @return
*/
bool isInRemoteServer();

public:
/**
* @brief 设置不透明度
Expand Down Expand Up @@ -462,37 +461,10 @@ private slots:
QString m_remotePassword;
//是否准备远程
bool m_remotePasswordIsReady = false;
};

static bool CanSplit(TermWidget *term, Qt::Orientation ori) {
qDebug() << "CanSplit:" << term << ori;
QSplitter *splitter = qobject_cast<QSplitter *>(term->parentWidget());
int minimumSize = ori == Qt::Horizontal ? TermWidget::MIN_WIDTH : TermWidget::MIN_HEIGHT;
if (splitter) {
if (splitter->orientation() == ori) {
QList<int> sizes = splitter->sizes();
// new term has same size portion as the current one.
sizes.append(sizes.at(splitter->indexOf(term)));

double sum = 0;
for (int i = 0; i < sizes.count(); i++) {
sum += sizes.at(i);
}

for(int i = 0; i < sizes.count(); i++) {
int totalSize = ori == Qt::Horizontal ? splitter->width() : splitter->height();
int actualSize = (totalSize) * (sizes.at(i) / sum);
if (actualSize < minimumSize)
return false;
}
} else {
int splitterSize = ori == Qt::Horizontal ? splitter->width() : splitter->height();
if (splitterSize / 2.0 < minimumSize)
return false;
}
}

return true;
}
// 9:6
static const int MIN_WIDTH = 180;
static const int MIN_HEIGHT = 120;
};

#endif // TERMWIDGET_H

0 comments on commit 084e086

Please sign in to comment.