Skip to content

Commit

Permalink
Traktor: Auto edit preview items implemented in PreviewList.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed May 27, 2024
1 parent 18f83e5 commit 912a5d7
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 33 deletions.
32 changes: 30 additions & 2 deletions code/Editor/App/DatabaseView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "Ui/TableLayout.h"
#include "Ui/HierarchicalState.h"
#include "Ui/Splitter.h"
#include "Ui/PreviewList/PreviewContentChangeEvent.h"
#include "Ui/PreviewList/PreviewItem.h"
#include "Ui/PreviewList/PreviewItems.h"
#include "Ui/PreviewList/PreviewList.h"
Expand Down Expand Up @@ -403,7 +404,7 @@ bool DatabaseView::create(ui::Widget* parent)
m_treeDatabase->addEventHandler< ui::TreeViewItemActivateEvent >(this, &DatabaseView::eventInstanceActivate);
m_treeDatabase->addEventHandler< ui::SelectionChangeEvent >(this, &DatabaseView::eventInstanceSelect);
m_treeDatabase->addEventHandler< ui::MouseButtonDownEvent >(this, &DatabaseView::eventInstanceButtonDown);
m_treeDatabase->addEventHandler< ui::TreeViewContentChangeEvent >(this, &DatabaseView::eventInstanceRenamed);
m_treeDatabase->addEventHandler< ui::TreeViewContentChangeEvent >(this, &DatabaseView::eventTreeContentChange);
m_treeDatabase->addEventHandler< ui::DragEvent >(this, &DatabaseView::eventInstanceDrag);
m_treeDatabase->setEnable(false);

Expand All @@ -412,6 +413,7 @@ bool DatabaseView::create(ui::Widget* parent)
return false;
m_listInstances->addEventHandler< ui::MouseButtonDownEvent >(this, &DatabaseView::eventInstanceButtonDown);
m_listInstances->addEventHandler< ui::MouseDoubleClickEvent >(this, &DatabaseView::eventInstancePreviewActivate);
m_listInstances->addEventHandler< ui::PreviewContentChangeEvent >(this, &DatabaseView::eventPreviewContentChange);
m_listInstances->addEventHandler< ui::DragEvent >(this, &DatabaseView::eventInstanceDrag);
m_listInstances->setVisible(false);

Expand Down Expand Up @@ -1637,7 +1639,7 @@ void DatabaseView::eventInstanceButtonDown(ui::MouseButtonDownEvent* event)
event->consume();
}

void DatabaseView::eventInstanceRenamed(ui::TreeViewContentChangeEvent* event)
void DatabaseView::eventTreeContentChange(ui::TreeViewContentChangeEvent* event)
{
Ref< ui::TreeViewItem > treeItem = event->getItem();
if (!treeItem)
Expand All @@ -1663,6 +1665,32 @@ void DatabaseView::eventInstanceRenamed(ui::TreeViewContentChangeEvent* event)
event->consume();
}

void DatabaseView::eventPreviewContentChange(ui::PreviewContentChangeEvent* event)
{
Ref< ui::PreviewItem > previewItem = event->getItem();
if (!previewItem)
return;

Ref< db::Instance > instance = previewItem->getData< db::Instance >(L"INSTANCE");
Ref< db::Group > group = previewItem->getData< db::Group >(L"GROUP");

bool result = false;

if (instance && group)
{
if (instance->checkout())
{
result = instance->setName(previewItem->getText());
result &= instance->commit();
}
}
else if (group)
result = group->rename(previewItem->getText());

if (result)
event->consume();
}

void DatabaseView::eventInstanceDrag(ui::DragEvent* event)
{
Ref< db::Instance > instance;
Expand Down
5 changes: 4 additions & 1 deletion code/Editor/App/DatabaseView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace traktor::ui
class Edit;
class HierarchicalState;
class Menu;
class PreviewContentChangeEvent;
class PreviewList;
class Splitter;
class ToolBar;
Expand Down Expand Up @@ -140,7 +141,9 @@ class DatabaseView : public ui::Container

void eventInstanceButtonDown(ui::MouseButtonDownEvent* event);

void eventInstanceRenamed(ui::TreeViewContentChangeEvent* event);
void eventTreeContentChange(ui::TreeViewContentChangeEvent* event);

void eventPreviewContentChange(ui::PreviewContentChangeEvent* event);

void eventInstanceDrag(ui::DragEvent* event);

Expand Down
33 changes: 33 additions & 0 deletions code/Ui/PreviewList/PreviewContentChangeEvent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* TRAKTOR
* Copyright (c) 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Ui/PreviewList/PreviewContentChangeEvent.h"

namespace traktor::ui
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.ui.PreviewContentChangeEvent", PreviewContentChangeEvent, ContentChangeEvent)

PreviewContentChangeEvent::PreviewContentChangeEvent(EventSubject* sender, PreviewItem* item, const std::wstring& originalText)
: ContentChangeEvent(sender)
, m_item(item)
, m_originalText(originalText)
{
}

PreviewItem* PreviewContentChangeEvent::getItem() const
{
return m_item;
}

const std::wstring& PreviewContentChangeEvent::getOriginalText() const
{
return m_originalText;
}

}
46 changes: 46 additions & 0 deletions code/Ui/PreviewList/PreviewContentChangeEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* TRAKTOR
* Copyright (c) 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once

#include <string>
#include "Ui/Events/ContentChangeEvent.h"

// import/export mechanism.
#undef T_DLLCLASS
#if defined(T_UI_EXPORT)
# define T_DLLCLASS T_DLLEXPORT
#else
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor::ui
{

class PreviewItem;

/*!
* \ingroup UI
*/
class T_DLLCLASS PreviewContentChangeEvent : public ContentChangeEvent
{
T_RTTI_CLASS;

public:
explicit PreviewContentChangeEvent(EventSubject* sender, PreviewItem* item, const std::wstring& originalText);

PreviewItem* getItem() const;

const std::wstring& getOriginalText() const;

private:
Ref< PreviewItem > m_item;
std::wstring m_originalText;
};

}
79 changes: 52 additions & 27 deletions code/Ui/PreviewList/PreviewItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Log/Log.h"
#include "Ui/Application.h"
#include "Ui/Bitmap.h"
#include "Ui/Canvas.h"
#include "Ui/StyleBitmap.h"
#include "Ui/StyleSheet.h"
#include "Ui/Auto/AutoWidget.h"
#include "Ui/PreviewList/PreviewItem.h"
#include "Ui/PreviewList/PreviewList.h"

namespace traktor::ui
{

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

PreviewItem::PreviewItem()
: m_selected(false)
{
m_imageBackground = new ui::StyleBitmap(L"UI.Preview.Background");
}

PreviewItem::PreviewItem(const std::wstring& text)
: m_text(text)
, m_selected(false)
{
m_imageBackground = new ui::StyleBitmap(L"UI.Preview.Background");
}
Expand Down Expand Up @@ -72,6 +72,42 @@ bool PreviewItem::isSelected() const
return m_selected;
}

void PreviewItem::mouseDown(MouseButtonDownEvent* event, const Point& position)
{
if (m_editable)
{
if (m_editMode == 0)
{
// Wait for next tap; cancel wait after 2 seconds.
getWidget()->requestInterval(this, 2000);
m_editMode = 1;
}
else if (m_editMode == 1)
{
// Double tap detected; begin edit after mouse is released.
getWidget()->requestInterval(this, 1000);
m_editMode = 2;
}
}
}

void PreviewItem::mouseUp(MouseButtonUpEvent* event, const Point& position)
{
PreviewList* list = getWidget< PreviewList >();
if (m_editMode == 2)
{
T_ASSERT(m_editable);
list->beginEdit(this);
m_editMode = 0;
}
}

void PreviewItem::mouseDoubleClick(MouseDoubleClickEvent* event, const Point& position)
{
// Ensure edit isn't triggered.
m_editMode = 0;
}

void PreviewItem::paint(Canvas& canvas, const Rect& rect)
{
const StyleSheet* ss = getStyleSheet();
Expand All @@ -82,9 +118,9 @@ void PreviewItem::paint(Canvas& canvas, const Rect& rect)
Size textExtent = canvas.getFontMetric().getExtent(text);
Size subTextExtent = !m_subText.empty() ? canvas.getFontMetric().getExtent(m_subText) : Size(0, 0);

Rect textRect = rect;
m_textRect = rect;

if (textExtent.cx > textRect.getWidth())
if (textExtent.cx > m_textRect.getWidth())
{
if (!isSelected())
{
Expand All @@ -93,26 +129,26 @@ void PreviewItem::paint(Canvas& canvas, const Rect& rect)
{
text = text.substr(0, text.length() - 1);
textExtent = canvas.getFontMetric().getExtent(text + L"...");
if (textExtent.cx <= textRect.getWidth())
if (textExtent.cx <= m_textRect.getWidth())
break;
}
text += L"...";
}
else
{
// Item is selected; enlarge text rectangle.
const int32_t excess = textExtent.cx - textRect.getWidth();
textRect.left -= excess / 2 + 5;
textRect.right += (excess + 1) / 2 + 5;
const int32_t excess = textExtent.cx - m_textRect.getWidth();
m_textRect.left -= excess / 2 + 5;
m_textRect.right += (excess + 1) / 2 + 5;
}
}

textRect.top = textRect.bottom - textExtent.cy - pixel(4_ut);
m_textRect.top = m_textRect.bottom - textExtent.cy - pixel(4_ut);
if (!m_subText.empty())
textRect.top -= subTextExtent.cy - pixel(4_ut);
m_textRect.top -= subTextExtent.cy - pixel(4_ut);

Rect previewRect = rect;
previewRect.bottom = textRect.top;
previewRect.bottom = m_textRect.top;

const int32_t dim = std::min(previewRect.getSize().cx, previewRect.getSize().cy) - pixel(8_ut);

Expand All @@ -124,18 +160,6 @@ void PreviewItem::paint(Canvas& canvas, const Rect& rect)

if (m_bitmapImage)
{
//if (m_imageBackground)
//{
// canvas.drawBitmap(
// thumbPosition,
// thumbSize,
// Point(0, 0),
// m_imageBackground->getSize(getWidget()),
// m_imageBackground,
// BlendMode::Opaque
// );
//}

canvas.drawBitmap(
thumbPosition,
thumbSize,
Expand All @@ -154,12 +178,12 @@ void PreviewItem::paint(Canvas& canvas, const Rect& rect)
if (isSelected())
{
canvas.setBackground(ss->getColor(this, L"background-color-selected"));
canvas.fillRect(textRect);
canvas.fillRect(m_textRect);
}

canvas.setForeground(ss->getColor(this, isSelected() ? L"color-selected" : L"color"));
canvas.drawText(
textRect,
m_textRect,
text,
AnCenter,
AnTop
Expand All @@ -173,11 +197,12 @@ void PreviewItem::paint(Canvas& canvas, const Rect& rect)
font.setSize((font.getSize()) * 2_ut / 3_ut);
canvas.setFont(font);

textRect.top += textExtent.cy + pixel(2_ut);
Rect subTextRect = m_textRect;
subTextRect.top += textExtent.cy + pixel(2_ut);

canvas.setForeground(ss->getColor(this, isSelected() ? L"color-selected" : L"color-subtext"));
canvas.drawText(
textRect,
subTextRect,
m_subText,
AnCenter,
AnTop
Expand Down
15 changes: 13 additions & 2 deletions code/Ui/PreviewList/PreviewItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,25 @@ class T_DLLCLASS PreviewItem : public AutoWidgetCell

bool isSelected() const;

virtual void paint(Canvas& canvas, const Rect& rect) override;
const Rect& getTextRect() const { return m_textRect; }

virtual void mouseDown(MouseButtonDownEvent* event, const Point& position) override final;

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

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

virtual void paint(Canvas& canvas, const Rect& rect) override final;

private:
std::wstring m_text;
std::wstring m_subText;
Ref< ui::StyleBitmap > m_imageBackground;
Ref< ui::IBitmap > m_bitmapImage;
bool m_selected;
Rect m_textRect;
bool m_selected = false;
bool m_editable = true;
int32_t m_editMode = 0;
};

}
Loading

0 comments on commit 912a5d7

Please sign in to comment.