Browse Source

Fixes #1 Use a Checkable dropdown componnent for choosing version to use

isundil 9 months ago
parent
commit
634d1f0c7c

+ 2 - 0
fakeRaid.vcxproj

@@ -12,6 +12,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="fakeRaid\browseModal.cpp" />
+    <ClCompile Include="fakeRaid\checkableComboBox.cpp" />
     <ClCompile Include="fakeRaid\conflictItemWidget.cpp" />
     <ClCompile Include="fakeRaid\conflictModal.cpp" />
     <ClCompile Include="fakeRaid\fileItem.cpp" />
@@ -32,6 +33,7 @@
     <ClInclude Include="fakeRaid\browseModal.h" />
     <QtMoc Include="fakeRaid\conflictItemWidget.h" />
     <QtMoc Include="fakeRaid\conflictModal.h" />
+    <QtMoc Include="fakeRaid\checkableComboBox.h" />
     <ClInclude Include="fakeRaid\exports.h" />
     <ClInclude Include="fakeRaid\fileItem.h" />
     <ClInclude Include="fakeRaid\htmlFont.hh" />

+ 6 - 0
fakeRaid.vcxproj.filters

@@ -44,6 +44,9 @@
     <ClCompile Include="fakeRaid\fileItem.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="fakeRaid\checkableComboBox.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <QtUic Include="fakeRaid\MainWindow.ui">
@@ -69,6 +72,9 @@
     <QtMoc Include="fakeRaid\conflictModal.h">
       <Filter>Header Files</Filter>
     </QtMoc>
+    <QtMoc Include="fakeRaid\checkableComboBox.h">
+      <Filter>Header Files</Filter>
+    </QtMoc>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="fakeRaid\exports.h">

+ 8 - 11
fakeRaid/ConflictItemWidget.ui

@@ -107,17 +107,7 @@
         </spacer>
        </item>
        <item>
-        <widget class="QComboBox" name="ddUseVersion"/>
-       </item>
-       <item>
-        <widget class="QPushButton" name="buttonUseVersion">
-         <property name="text">
-          <string>Use Version</string>
-         </property>
-         <property name="checkable">
-          <bool>true</bool>
-         </property>
-        </widget>
+        <widget class="CheckableComboBox" name="ddUseVersion"/>
        </item>
        <item>
         <widget class="QPushButton" name="buttonCopy">
@@ -155,6 +145,13 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>CheckableComboBox</class>
+   <extends>QComboBox</extends>
+   <header>checkablecombobox.h</header>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>

+ 61 - 0
fakeRaid/checkableComboBox.cpp

@@ -0,0 +1,61 @@
+#include <QStylePainter>
+#include "checkableComboBox.h"
+
+CheckableComboBox::CheckableComboBox(QWidget* parent): QComboBox(parent)
+{
+}
+
+void CheckableComboBox::setChecked(bool val)
+{
+    if (checked != val)
+    {
+        checked = val;
+        this->update();
+    }
+}
+
+bool CheckableComboBox::isChecked() const
+{
+	return checked;
+}
+
+void CheckableComboBox::paintEvent(QPaintEvent* e)
+{
+    QStylePainter painter(this);
+    painter.setPen(palette().color(QPalette::Text));
+    QStyleOptionComboBox opt;
+    initStyleOption(&opt);
+
+    if (checked)
+    {
+        QStyleOptionButton option;
+        QRectF downButtonRect = style()->proxy()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this);
+        option.initFrom(this);
+        option.features = QStyleOptionButton::HasMenu;
+        option.state |= QStyle::State_On;
+        option.text = currentText();
+        option.icon = qvariant_cast<QIcon>(currentData(Qt::DecorationRole));
+        option.iconSize = iconSize();
+
+        // Draw button
+        option.rect.adjust(0, 0, downButtonRect.width() +4, 0);
+        painter.drawControl(QStyle::CE_PushButtonBevel, option);
+
+        // Draw button text
+        option.rect.adjust(0, 0, -downButtonRect.width() -4, 0);
+        painter.drawControl(QStyle::CE_PushButtonLabel, option);
+
+        // Draw dropdown button
+        painter.setPen(palette().text().color());
+        painter.drawText(downButtonRect, "\uE019", Qt::AlignVCenter | Qt::AlignHCenter);
+    }
+    else
+    {
+        if (currentIndex() < 0 && !placeholderText().isEmpty()) {
+            opt.palette.setBrush(QPalette::ButtonText, opt.palette.placeholderText());
+            opt.currentText = placeholderText();
+        }
+        painter.drawComplexControl(QStyle::CC_ComboBox, opt);
+        painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
+    }
+}

+ 18 - 0
fakeRaid/checkableComboBox.h

@@ -0,0 +1,18 @@
+#pragma once
+
+#include <QComboBox>
+
+class CheckableComboBox : public QComboBox
+{
+	Q_OBJECT
+	public:
+		CheckableComboBox(QWidget* parent =nullptr);
+
+		void paintEvent(QPaintEvent* e) override;
+
+		void setChecked(bool value);
+		bool isChecked() const;
+
+	private:
+		bool checked =false;
+};

+ 5 - 7
fakeRaid/conflictItemWidget.cpp

@@ -14,7 +14,7 @@ ConflictItemWidget::ConflictItemWidget(QListWidget* parent, const QFile& file, c
 	ui.fileIcon->setPixmap(IconProvider::FromFile(file, ui.fileIcon->baseSize()));
 	ui.filename->setText(p.filename().string().c_str());
 
-	connect(ui.buttonUseVersion, &QPushButton::clicked, this, [this]() { SetAction(Action::UseVersion); });
+	connect(ui.ddUseVersion, &CheckableComboBox::activated, this, [this]() { SetAction(Action::UseVersion); });
 	connect(ui.buttonCopy, &QPushButton::clicked, this, [this]() { SetAction(Action::Copy); });
 	connect(ui.buttonRemove, &QPushButton::clicked, this, [this]() { SetAction(Action::Remove); });
 	connect(ui.buttonIgnore, &QPushButton::clicked, this, [this]() { SetAction(Action::Ignore); });
@@ -22,12 +22,12 @@ ConflictItemWidget::ConflictItemWidget(QListWidget* parent, const QFile& file, c
 
 void ConflictItemWidget::SetAction(Action&& action)
 {
+	ui.ddUseVersion->setChecked(action == Action::UseVersion);
+	ui.buttonCopy->setChecked(action == Action::Copy);
+	ui.buttonRemove->setChecked(action == Action::Remove);
+	ui.buttonIgnore->setChecked(action == Action::Ignore);
 	if (currentAction != action)
 	{
-		ui.buttonUseVersion->setChecked(action == Action::UseVersion);
-		ui.buttonCopy->setChecked(action == Action::Copy);
-		ui.buttonRemove->setChecked(action == Action::Remove);
-		ui.buttonIgnore->setChecked(action == Action::Ignore);
 		currentAction = action;
 		emit ActionChanged();
 	}
@@ -110,7 +110,6 @@ ConflictItemWidget* ConflictItemWidget::FromMissingFile(QListWidget* parent, con
 {
 	const std::string fullPathFirstFound = rootPaths[std::distance(version.begin(), std::find(version.begin(), version.end(), true))] + "/" + filename;
 	ConflictItemWidget* result = new ConflictItemWidget(parent, QFile(fullPathFirstFound.c_str()), filename);
-	result->ui.buttonUseVersion->setVisible(false);
 	result->ui.ddUseVersion->setVisible(false);
 	std::stringstream ss;
 	ss << "File " << HTMLFont(filename, FILE_STYLE) << " does not exists in every repositories: <ul>";
@@ -124,7 +123,6 @@ ConflictItemWidget* ConflictItemWidget::FromMissingDir(QListWidget* parent, cons
 {
 	const std::string fullPathFirstFound = rootPaths[std::distance(version.begin(), std::find(version.begin(), version.end(), true))] + "/" + filename;
 	ConflictItemWidget* result = new ConflictItemWidget(parent, QFile(fullPathFirstFound.c_str()), filename);
-	result->ui.buttonUseVersion->setVisible(false);
 	result->ui.ddUseVersion->setVisible(false);
 	std::stringstream ss;
 	ss << "Folder " << HTMLFont(filename, FILE_STYLE) << " does not exists in every repositories: <ul>";