Sandboxie/SandboxiePlus/MiscHelpers/Common/SettingsWidgets.cpp

311 lines
9.3 KiB
C++
Raw Normal View History

2021-10-16 16:19:51 +01:00
#include "stdafx.h"
#include "SettingsWidgets.h"
#include "CheckableMessageBox.h"
2022-08-15 12:18:26 +01:00
#include "NeonEffect.h"
2021-10-16 16:19:51 +01:00
///////////////////////////////////////////////////
// CPathEdit
2023-03-31 21:05:46 +01:00
CPathEdit::CPathEdit(bool bDirs, bool bCombo, QWidget *parent)
2021-10-16 16:19:51 +01:00
: CTxtEdit(parent)
{
m_bDirs = bDirs;
QHBoxLayout* pLayout = new QHBoxLayout(this);
2022-09-29 17:28:48 +01:00
pLayout->setContentsMargins(0,0,0,0);
2023-03-31 21:05:46 +01:00
if (bCombo) {
m_pCombo = new QComboBox();
m_pCombo->setEditable(true);
m_pEdit = m_pCombo->lineEdit();
pLayout->addWidget(m_pCombo);
}
else {
m_pEdit = new QLineEdit(this);
pLayout->addWidget(m_pEdit);
}
2021-10-16 16:19:51 +01:00
connect(m_pEdit, SIGNAL(textChanged(const QString &)), this, SIGNAL(textChanged(const QString &)));
QPushButton* pButton = new QPushButton("...");
pButton->setMaximumWidth(25);
connect(pButton, SIGNAL(clicked(bool)), this, SLOT(Browse()));
pLayout->addWidget(pButton);
2023-03-31 21:05:46 +01:00
m_Filter = tr("Any File (*.*)");
2021-10-16 16:19:51 +01:00
}
void CPathEdit::Browse()
{
QString FilePath = m_bDirs
? QFileDialog::getExistingDirectory(this, tr("Select Directory"))
2023-03-31 21:05:46 +01:00
: QFileDialog::getOpenFileName(0, tr("Browse"), "", m_Filter);
2021-10-16 16:19:51 +01:00
if (FilePath.isEmpty())
return;
if (m_bWinPath)
FilePath.replace("/", "\\");
SetText(FilePath);
}
///////////////////////////////////////////////////
// CProxyEdit
CProxyEdit::CProxyEdit(QWidget *parent)
: CTxtEdit(parent)
{
QHBoxLayout* pLayout = new QHBoxLayout(this);
2022-09-29 17:28:48 +01:00
pLayout->setContentsMargins(0,0,0,0);
2021-10-16 16:19:51 +01:00
m_pType = new QComboBox();
m_pType->addItem(QString("No"));
m_pType->addItem(QString("http"));
m_pType->addItem(QString("socks5"));
connect(m_pType, SIGNAL(activated(int)), this, SLOT(OnType(int)));
pLayout->addWidget(m_pType);
m_pEdit = new QLineEdit(this);
connect(m_pEdit, SIGNAL(textChanged(const QString &)), this, SIGNAL(textChanged(const QString &)));
pLayout->addWidget(m_pEdit);
}
void CProxyEdit::SetText(const QString& Text)
{
QUrl Url(Text);
m_pType->setCurrentText(Url.scheme());
m_pEdit->setText(Text);
}
void CProxyEdit::OnType(int Index)
{
if(Index == 0)
m_pEdit->setEnabled(false);
else
{
m_pEdit->setEnabled(true);
m_pEdit->setText(m_pType->currentText() + "://");
}
}
2022-08-15 12:18:26 +01:00
//////////////////////////////////////////////////////////////////////////
// CConfigDialog
CConfigDialog::CConfigDialog(QWidget* parent)
: QDialog(parent)
{
2022-08-15 18:32:38 +01:00
m_pTabs = NULL;
2022-08-15 12:18:26 +01:00
m_pStack = NULL;
2022-08-15 12:53:50 +01:00
m_pSearch = NULL;
2022-08-15 12:18:26 +01:00
m_pTree = NULL;
2022-08-15 18:32:38 +01:00
m_SearchI = m_SearchJ = m_SearchP = 0;
m_LastFound = NULL;
2022-08-15 12:18:26 +01:00
}
QWidget* CConfigDialog::ConvertToTree(QTabWidget* pTabWidget)
{
QWidget* pAltView = new QWidget(this);
QGridLayout* pLayout = new QGridLayout(pAltView);
pLayout->setContentsMargins(0, 0, 0, 0);
m_pTree = new QTreeWidget();
m_pTree->setHeaderHidden(true);
2022-09-29 17:28:48 +01:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
m_pTree->setMinimumWidth(200.0*(QApplication::desktop()->logicalDpiX()/96.0)); // todo Qt6
#endif
2022-08-15 12:18:26 +01:00
QStyle* pStyle = QStyleFactory::create("windows"); // show lines
m_pTree->setStyle(pStyle);
connect(m_pTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnItemClicked(QTreeWidgetItem*, int)));
2022-08-15 12:53:50 +01:00
m_pSearch = new QLineEdit();
QObject::connect(m_pSearch, SIGNAL(returnPressed()), this, SLOT(OnSearchOption()));
pLayout->addWidget(m_pSearch, 0, 0);
2022-08-15 12:18:26 +01:00
pLayout->addWidget(m_pTree, 1, 0);
m_pStack = new QStackedLayout();
m_pStack->setContentsMargins(0, 0, 0, 0);
pLayout->addLayout(m_pStack, 0, 1, 2, 1);
for (int i = 0, k = 0; i < pTabWidget->count(); i++, k++) {
QTreeWidgetItem* pItem = new QTreeWidgetItem(QStringList() << pTabWidget->tabText(i));
m_pTree->addTopLevelItem(pItem);
2022-09-29 17:28:48 +01:00
//pItem->setData(1, Qt::UserRole, k);
pItem->setData(1, Qt::UserRole, m_pStack->count());
2022-08-15 12:18:26 +01:00
pItem->setIcon(0, pTabWidget->tabIcon(i));
QGridLayout* pGrid = qobject_cast<QGridLayout*>(pTabWidget->widget(i)->layout());
2022-08-15 18:32:38 +01:00
QTabWidget* pSubTabs = pGrid ? qobject_cast<QTabWidget*>(pGrid->itemAt(0)->widget()) : NULL;
if (!pSubTabs) {
2022-08-15 12:18:26 +01:00
pItem->setData(0, Qt::UserRole, m_pStack->count());
m_pStack->addWidget(pTabWidget->widget(i--));
}
else {
//pItem->setData(0, Qt::UserRole, -1);
//pItem->setFlags(pItem->flags() & ~Qt::ItemIsSelectable);
pItem->setData(0, Qt::UserRole, m_pStack->count()); // take the first tab for the parent entry
2022-08-15 18:32:38 +01:00
for (int j = 0; j < pSubTabs->count(); j++) {
QTreeWidgetItem* pSubItem = new QTreeWidgetItem(QStringList() << pSubTabs->tabText(j));
2022-08-15 12:18:26 +01:00
pItem->addChild(pSubItem);
pSubItem->setData(0, Qt::UserRole, m_pStack->count());
2022-08-20 13:48:18 +01:00
pSubItem->setIcon(0, pSubTabs->tabIcon(j));
2022-08-15 18:32:38 +01:00
m_pStack->addWidget(pSubTabs->widget(j--));
2022-08-15 12:18:26 +01:00
}
}
}
m_pTree->expandAll();
2022-09-29 17:28:48 +01:00
m_pTree->resizeColumnToContents(0);
//m_pTree->setMinimumWidth(m_pTree->columnWidth(0));
//m_pTree->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
//m_pTree->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
2022-08-15 12:18:26 +01:00
pTabWidget->clear();
return pAltView;
}
2022-08-15 18:32:38 +01:00
void CConfigDialog::OnItemClicked(QTreeWidgetItem* pItem, int Column)
{
int Index = pItem->data(0, Qt::UserRole).toInt();
if (Index != -1)
m_pStack->setCurrentIndex(Index);
2022-09-29 17:28:48 +01:00
//QTreeWidgetItem* pRootItem = pItem;
//while (pRootItem->parent()) pRootItem = pRootItem->parent();
//int RootIndex = pRootItem->data(1, Qt::UserRole).toInt();
//QWidget* pWidget = m_pStack->widget(RootIndex);
QWidget* pWidget = m_pStack->widget(Index);
qDebug() << pWidget->objectName();
if (m_pCurrentTab != pWidget)
OnTab(pWidget);
2022-08-15 18:32:38 +01:00
}
2022-08-15 12:18:26 +01:00
template <class T>
bool CConfigDialog__CompareText(T pWidget, const QString& Text) {
QString Str = pWidget->text();
if (!Str.toLower().contains(Text))
return false;
2022-08-16 10:43:55 +01:00
//qDebug() << Str;
2022-08-15 12:18:26 +01:00
return true;
}
QWidget* CConfigDialog__SearchWidget(QWidget* pParent, const QString& Text, int& Pos)
{
QList<QWidget*> Widgets = pParent->findChildren<QWidget*>();
for (Pos; Pos < Widgets.count(); Pos++) {
QWidget* pWidget = Widgets[Pos];
if (!pWidget->isHidden()) {
if (QCheckBox* pCheck = qobject_cast<QCheckBox*>(pWidget)) {
if (CConfigDialog__CompareText(pCheck, Text))
return pCheck;
}
else if (QLabel* pLabel = qobject_cast<QLabel*>(pWidget)) {
if (CConfigDialog__CompareText(pLabel, Text))
return pLabel;
}
}
}
Pos = 0;
return NULL;
}
2022-08-15 18:32:38 +01:00
QWidget* CConfigDialog__SearchTree(QTreeWidget* pTree, QStackedLayout* pStack, const QString& Text, int& I, int& J, int& Pos)
2022-08-15 12:18:26 +01:00
{
for (; I < pTree->topLevelItemCount(); I++) {
QTreeWidgetItem* pItem = pTree->topLevelItem(I);
if (pItem->childCount() == 0) {
int Index = pItem->data(0, Qt::UserRole).toInt();
if (QWidget* pWidget = CConfigDialog__SearchWidget(pStack->widget(Index), Text, Pos))
return pWidget;
}
else {
for (; J < pItem->childCount(); J++) {
QTreeWidgetItem* pSubItem = pItem->child(J);
int Index = pSubItem->data(0, Qt::UserRole).toInt();
if (QWidget* pWidget = CConfigDialog__SearchWidget(pStack->widget(Index), Text, Pos))
return pWidget;
}
J = 0;
}
}
I = 0;
return NULL;
}
2022-08-15 18:32:38 +01:00
QWidget* CConfigDialog__SearchTabs(QTabWidget* pTabWidget, const QString& Text, int& I, int& J, int& Pos)
{
for (; I < pTabWidget->count(); I++) {
QGridLayout* pGrid = qobject_cast<QGridLayout*>(pTabWidget->widget(I)->layout());
QTabWidget* pSubTabs = pGrid ? qobject_cast<QTabWidget*>(pGrid->itemAt(0)->widget()) : NULL;
if (!pSubTabs) {
QWidget* parent = pTabWidget->widget(I);
if (QWidget* pWidget = CConfigDialog__SearchWidget(parent, Text, Pos))
return pWidget;
}
else {
for (; J < pSubTabs->count(); J++) {
QWidget* parent = pSubTabs->widget(J);
if (QWidget* pWidget = CConfigDialog__SearchWidget(parent, Text, Pos))
return pWidget;
}
J = 0;
}
}
I = 0;
return NULL;
}
QWidget* CConfigDialog::AddConfigSearch(QTabWidget* pTabs)
{
m_pTabs = pTabs;
m_pSearch = new QLineEdit();
QObject::connect(m_pSearch, SIGNAL(returnPressed()), this, SLOT(OnSearchOption()));
m_pSearch->setMaximumWidth(150);
return m_pSearch;
}
2022-08-15 12:18:26 +01:00
void CConfigDialog::OnSearchOption()
{
2022-08-15 12:53:50 +01:00
QString Text = m_pSearch->text().toLower();
2022-08-15 12:18:26 +01:00
if (m_LastFound) {
//m_LastFound->setPalette(QApplication::palette());
//m_LastFound->setAutoFillBackground(false);
m_LastFound->setGraphicsEffect(NULL);
}
2022-08-15 18:32:38 +01:00
QWidget* pWidget = NULL;
if (m_pTree) {
pWidget = CConfigDialog__SearchTree(m_pTree, m_pStack, Text, m_SearchI, m_SearchJ, m_SearchP);
if (pWidget) {
QTreeWidgetItem* pItem = m_pTree->topLevelItem(m_SearchI);
if (pItem && pItem->childCount() > 0)
pItem = pItem->child(m_SearchJ);
if (!pItem) return;
//m_pTree->setItemSelected(pItem, true);
m_pTree->setCurrentItem(pItem, 0);
OnItemClicked(pItem, 0);
}
}
else if(m_pTabs) {
pWidget = CConfigDialog__SearchTabs(m_pTabs, Text, m_SearchI, m_SearchJ, m_SearchP);
if (pWidget) {
QGridLayout* pGrid = qobject_cast<QGridLayout*>(m_pTabs->widget(m_SearchI)->layout());
QTabWidget* pSubTabs = pGrid ? qobject_cast<QTabWidget*>(pGrid->itemAt(0)->widget()) : NULL;
m_pTabs->setCurrentIndex(m_SearchI);
if (pSubTabs) pSubTabs->setCurrentIndex(m_SearchJ);
}
}
2022-08-15 12:18:26 +01:00
if (!pWidget) {
QApplication::beep();
return;
}
m_SearchP++; // move index to the next for the next search
m_LastFound = pWidget;
//QPalette palette = m_LastFound->palette();
//palette.setColor(QPalette::Button, Qt::red);
//palette.setColor(QPalette::Base, Qt::red);
//palette.setColor(QPalette::Window, Qt::red);
//m_LastFound->setAutoFillBackground(true);
//m_LastFound->setPalette(palette);
auto neon = new CNeonEffect(5, 4);
neon->setGlowColor(Qt::red);
//neon->setColor(Qt::black);
m_LastFound->setGraphicsEffect(neon);
}