Skip to content

Commit

Permalink
Traktor: Auto fix grid column width in GridView implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed May 8, 2024
1 parent 5e69874 commit 991068a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
31 changes: 24 additions & 7 deletions code/Ui/GridView/GridHeader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,14 +9,12 @@
#include "Ui/Application.h"
#include "Ui/Canvas.h"
#include "Ui/StyleSheet.h"
#include "Ui/Auto/AutoWidget.h"
#include "Ui/GridView/GridHeader.h"
#include "Ui/GridView/GridColumn.h"
#include "Ui/GridView/GridView.h"

namespace traktor
namespace traktor::ui
{
namespace ui
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.ui.GridHeader", GridHeader, AutoWidgetCell)

Expand All @@ -27,7 +25,7 @@ void GridHeader::setColumns(const RefArray< GridColumn >& columns)

void GridHeader::mouseDown(MouseButtonDownEvent* event, const Point& position)
{
if (m_columns.size() < 2)
if (event->getButton() != MbtLeft || m_columns.size() < 2)
return;

int32_t dx = pixel(2_ut);
Expand All @@ -52,6 +50,26 @@ void GridHeader::mouseUp(MouseButtonUpEvent* event, const Point& position)
m_resizeColumn = 0;
}

void GridHeader::mouseDoubleClick(MouseDoubleClickEvent* event, const Point& position)
{
if (event->getButton() != MbtLeft || m_columns.size() < 2)
return;

int32_t dx = pixel(2_ut);
int32_t x = 0;

for (uint32_t i = 0; i < m_columns.size(); ++i)
{
GridColumn* column = m_columns[i];
x += pixel(column->getWidth());
if (position.x >= x - dx && position.x <= x + dx)
{
getWidget< GridView >()->fitColumn(i);
break;
}
}
}

void GridHeader::mouseMove(MouseMoveEvent* event, const Point& position)
{
if (!m_resizeColumn)
Expand Down Expand Up @@ -100,5 +118,4 @@ void GridHeader::paint(Canvas& canvas, const Rect& rect)
canvas.drawLine(left, rect.top + 4, left, rect.bottom - 4);
}

}
}
4 changes: 3 additions & 1 deletion code/Ui/GridView/GridHeader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -35,6 +35,8 @@ class T_DLLCLASS GridHeader : public AutoWidgetCell

virtual void mouseUp(MouseButtonUpEvent* event, const Point& position) override final;

virtual void mouseDoubleClick(MouseDoubleClickEvent* event, const Point& position) override final;

virtual void mouseMove(MouseMoveEvent* event, const Point& position) override final;

virtual void paint(Canvas& canvas, const Rect& rect) override final;
Expand Down
8 changes: 4 additions & 4 deletions code/Ui/GridView/GridItem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -135,13 +135,13 @@ AutoWidgetCell* GridItem::hitTest(const Point& position)
void GridItem::paint(Canvas& canvas, const Rect& rect)
{
const StyleSheet* ss = getStyleSheet();
Rect rcText(rect.left + 2, rect.top, rect.right, rect.bottom);
Rect rcText(rect.left + pixel(2_ut), rect.top, rect.right, rect.bottom);

if (m_text.empty())
{
int32_t w = 0;
for (auto image : m_images)
w += image->getSize(getWidget()).cx + 2;
w += image->getSize(getWidget()).cx + pixel(2_ut);
rcText.left += (rcText.getWidth() - w) / 2;
}

Expand All @@ -161,7 +161,7 @@ void GridItem::paint(Canvas& canvas, const Rect& rect)
BlendMode::Alpha
);

rcText.left += szImage.cx + 2;
rcText.left += szImage.cx + pixel(2_ut);
}

if (!m_text.empty())
Expand Down
26 changes: 23 additions & 3 deletions code/Ui/GridView/GridView.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma optimize( "", off )

/*
* TRAKTOR
* Copyright (c) 2022-2023 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -278,6 +276,28 @@ void GridView::setMultiSelect(bool multiSelect)
m_multiSelect = multiSelect;
}

void GridView::fitColumn(int32_t columnIndex)
{
if (columnIndex < 0 || columnIndex >= (int32_t)m_columns.size())
return;

const auto fm = getFontMetric();

int maxWidth = pixel(16_ut);
for (auto row : getRows(GfDescendants))
{
const GridItem* item = row->get(columnIndex);
if (item)
{
const int32_t width = fm.getExtent(item->getText()).cx;
maxWidth = std::max(maxWidth, width);
}
}

m_columns[columnIndex]->setWidth(unit(maxWidth) + 4_ut);
requestUpdate();
}

Ref< HierarchicalState > GridView::captureState() const
{
Ref< HierarchicalState > state = new HierarchicalState();
Expand Down
2 changes: 2 additions & 0 deletions code/Ui/GridView/GridView.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class T_DLLCLASS GridView : public AutoWidget

void setMultiSelect(bool multiSelect);

void fitColumn(int32_t columnIndex);

Ref< HierarchicalState > captureState() const;

void applyState(const HierarchicalState* state);
Expand Down

0 comments on commit 991068a

Please sign in to comment.