Commit 8a337d1c authored by captainwong's avatar captainwong

jlibqt/TextMenu

parent d850d100
#include "TextMenu.h"
#include "CheckBtn.h"
#include "../QtUtils.h"
#include "../QtStyleSheet.h"
using namespace jlib::qt;
TextMenu::TextMenu(QWidget *parent, int font_sz)
: BaseScrollView(parent)
, font_sz_(font_sz)
{
}
TextMenu::~TextMenu()
{
}
void TextMenu::add_menu(int tag, QString text)
{
auto chk = new CheckBtn(root(), tag);
chk->setText(text);
chk->set_font_size(font_sz_);
chk->refresh();
menu_items_.push_back(chk);
connect(chk, SIGNAL(sig_check(int, bool)), this, SLOT(slot_check(int, bool)));
}
void TextMenu::refresh(QPoint pt)
{
if (menu_items_.empty())return;
int gap = 2;
auto sample_chk = menu_items_.front();
int max_width = sample_chk->width();
int max_y = parentWidget()->rect().bottom();
int y = 0;
for (auto& chk : menu_items_) {
chk->move(0, y);
y += chk->height() + gap;
if (height() < y && pt.y() + y < max_y) {
setFixedHeight(y);
}
if (root()->height() < y) {
root()->setFixedHeight(y);
}
if (max_width < chk->width()) {
max_width = chk->width();
root()->setFixedWidth(max_width);
}
MYQDEBUG << "max_width" << max_width;
}
setFixedWidth(max_width + 10);
move(pt.x(), pt.y());
adjustSize();
setFocus();
}
bool TextMenu::focus_down()
{
if (!menu_items_.empty()) {
if (++focus_ == menu_items_.size()) {
focus_--;
}
MYQDEBUG << "focus_=" << focus_;
auto iter = menu_items_.begin();
for (int i = 0; i < focus_; i++) {
iter++;
}
auto item = *iter;
ensureWidgetVisible(item);
auto pt = item->mapToGlobal(item->rect().center());
SetCursorPos(pt.x(), pt.y());
return true;
}
return false;
}
bool TextMenu::focus_up()
{
if (!menu_items_.empty()) {
if (--focus_ < 0) {
focus_ = 0;
}
MYQDEBUG << "focus_=" << focus_;
auto iter = menu_items_.begin();
for (int i = 0; i < focus_; i++) {
iter++;
}
auto item = *iter;
ensureWidgetVisible(item);
auto pt = item->mapToGlobal(item->rect().center());
SetCursorPos(pt.x(), pt.y());
return true;
}
return false;
}
void TextMenu::click_focus()
{
auto iter = menu_items_.begin();
for (int i = 0; i < focus_ && iter != menu_items_.end(); i++) {
iter++;
}
if (iter != menu_items_.end()) {
auto item = *iter;
slot_check(item->tag(), true);
}
}
void TextMenu::focusOutEvent(QFocusEvent * e)
{
auto top = topLevelWidget();
if (isAncestorOf(top)) {
return;
}
hide();
emit sig_menu_dead();
deleteLater();
}
void TextMenu::slot_check(int tag, bool is_check)
{
if (!is_check)return;
MYQDEBUG << tag << is_check;
emit sig_menu_check(tag);
parentWidget()->setFocus();
}
#pragma once
#include <QtWidgets>
#include <vector>
#include "BaseScrollView.h"
class CheckBtn;
class TextMenu : public BaseScrollView
{
Q_OBJECT
public:
TextMenu(QWidget *parent, int font_sz);
~TextMenu();
void add_menu(int tag, QString text);
void refresh(QPoint pt);
// return true for success, false for back
bool focus_down();
// return true for success, false for back
bool focus_up();
void click_focus();
protected:
virtual void focusOutEvent(QFocusEvent* e) override;
signals:
void sig_menu_check(int tag);
void sig_menu_dead();
private slots:
void slot_check(int tag, bool is_check);
private:
int sel_ = -1;
int focus_ = -1;
std::vector<CheckBtn*> menu_items_ = {};
int font_sz_ = 18;
};
......@@ -97,12 +97,14 @@
<ClCompile Include="View\BgColorBtn.cpp" />
<ClCompile Include="View\CheckBtn.cpp" />
<ClCompile Include="View\IconBtn.cpp" />
<ClCompile Include="View\TextMenu.cpp" />
<ClCompile Include="View\TitleBar.cpp" />
<ClCompile Include="View\PageView.cpp" />
<ClCompile Include="View\RndButton.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<QtMoc Include="View\TextMenu.h" />
<QtMoc Include="View\PageView.h" />
<QtMoc Include="View\CheckBtn.h" />
<QtMoc Include="View\BaseScrollView.h" />
......
......@@ -54,6 +54,9 @@
<ClCompile Include="View\TitleBar.cpp">
<Filter>View</Filter>
</ClCompile>
<ClCompile Include="View\TextMenu.cpp">
<Filter>View</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="qt.h">
......@@ -114,5 +117,8 @@
<QtMoc Include="View\TitleBar.h">
<Filter>View</Filter>
</QtMoc>
<QtMoc Include="View\TextMenu.h">
<Filter>View</Filter>
</QtMoc>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment