1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-12-21 16:11:11 +01:00

Generalize TimeRenderer and TimeEditor to handle frequencies in grid

This commit is contained in:
Paul Licameli
2016-06-26 19:05:06 -04:00
parent 8943f682f2
commit 9c687f055c
4 changed files with 87 additions and 69 deletions

View File

@@ -163,7 +163,8 @@ LabelDialog::LabelDialog(wxWindow *parent,
// do it for us. (The DecRef() that is needed after GetDefaultEditorForType // do it for us. (The DecRef() that is needed after GetDefaultEditorForType
// becomes the duty of the wxGridCellAttr objects after we set them in the grid.) // becomes the duty of the wxGridCellAttr objects after we set them in the grid.)
mChoiceEditor = (ChoiceEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_CHOICE); mChoiceEditor = (ChoiceEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_CHOICE);
mTimeEditor = (TimeEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_TIME); mTimeEditor = static_cast<NumericEditor*>
(mGrid->GetDefaultEditorForType(GRID_VALUE_TIME));
// Initialize and set the track name column attributes // Initialize and set the track name column attributes
wxGridCellAttr *attr; wxGridCellAttr *attr;

View File

@@ -80,7 +80,7 @@ class LabelDialog final : public wxDialog
Grid *mGrid; Grid *mGrid;
ChoiceEditor *mChoiceEditor; ChoiceEditor *mChoiceEditor;
TimeEditor *mTimeEditor; NumericEditor *mTimeEditor;
RowDataArray mData; RowDataArray mData;

View File

@@ -25,27 +25,25 @@
#include "Grid.h" #include "Grid.h"
#include "NumericTextCtrl.h" #include "NumericTextCtrl.h"
#include "../SelectedRegion.h"
TimeEditor::TimeEditor() NumericEditor::NumericEditor
{ (NumericConverter::Type type, const wxString &format, double rate)
TimeEditor(wxT("seconds"), 44100);
}
TimeEditor::TimeEditor(const wxString &format, double rate)
{ {
mType = type;
mFormat = format; mFormat = format;
mRate = rate; mRate = rate;
mOld = 0.0; mOld = 0.0;
} }
TimeEditor::~TimeEditor() NumericEditor::~NumericEditor()
{ {
} }
void TimeEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) void NumericEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler)
{ {
wxASSERT(parent); // to justify safenew wxASSERT(parent); // to justify safenew
m_control = safenew NumericTextCtrl(NumericConverter::TIME, parent, auto control = safenew NumericTextCtrl(mType, parent,
wxID_ANY, wxID_ANY,
mFormat, mFormat,
mOld, mOld,
@@ -53,11 +51,14 @@ void TimeEditor::Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler)
wxDefaultPosition, wxDefaultPosition,
wxDefaultSize, wxDefaultSize,
true); true);
if (mType == NumericTextCtrl::FREQUENCY)
control->SetInvalidValue(SelectedRegion::UndefinedFrequency);
m_control = control;
wxGridCellEditor::Create(parent, id, handler); wxGridCellEditor::Create(parent, id, handler);
} }
void TimeEditor::SetSize(const wxRect &rect) void NumericEditor::SetSize(const wxRect &rect)
{ {
wxSize size = m_control->GetSize(); wxSize size = m_control->GetSize();
@@ -68,22 +69,24 @@ void TimeEditor::SetSize(const wxRect &rect)
m_control->Move(x, y); m_control->Move(x, y);
} }
void TimeEditor::BeginEdit(int row, int col, wxGrid *grid) void NumericEditor::BeginEdit(int row, int col, wxGrid *grid)
{ {
wxGridTableBase *table = grid->GetTable(); wxGridTableBase *table = grid->GetTable();
mOldString = table->GetValue(row, col); mOldString = table->GetValue(row, col);
mOldString.ToDouble(&mOld); mOldString.ToDouble(&mOld);
GetTimeCtrl()->SetValue(mOld); auto control = GetNumericTextControl();
GetTimeCtrl()->EnableMenu(); control->SetValue(mOld);
control->EnableMenu();
GetTimeCtrl()->SetFocus(); control->SetFocus();
} }
bool TimeEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXUNUSED(grid), const wxString &WXUNUSED(oldval), wxString *newval)
bool NumericEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXUNUSED(grid), const wxString &WXUNUSED(oldval), wxString *newval)
{ {
double newtime = GetTimeCtrl()->GetValue(); double newtime = GetNumericTextControl()->GetValue();
bool changed = newtime != mOld; bool changed = newtime != mOld;
if (changed) { if (changed) {
@@ -94,17 +97,17 @@ bool TimeEditor::EndEdit(int WXUNUSED(row), int WXUNUSED(col), const wxGrid *WXU
return changed; return changed;
} }
void TimeEditor::ApplyEdit(int row, int col, wxGrid *grid) void NumericEditor::ApplyEdit(int row, int col, wxGrid *grid)
{ {
grid->GetTable()->SetValue(row, col, mValueAsString); grid->GetTable()->SetValue(row, col, mValueAsString);
} }
void TimeEditor::Reset() void NumericEditor::Reset()
{ {
GetTimeCtrl()->SetValue(mOld); GetNumericTextControl()->SetValue(mOld);
} }
bool TimeEditor::IsAcceptedKey(wxKeyEvent &event) bool NumericEditor::IsAcceptedKey(wxKeyEvent &event)
{ {
if (wxGridCellEditor::IsAcceptedKey(event)) { if (wxGridCellEditor::IsAcceptedKey(event)) {
if (event.GetKeyCode() == WXK_RETURN) { if (event.GetKeyCode() == WXK_RETURN) {
@@ -116,41 +119,41 @@ bool TimeEditor::IsAcceptedKey(wxKeyEvent &event)
} }
// Clone is required by wxwidgets; implemented via copy constructor // Clone is required by wxwidgets; implemented via copy constructor
wxGridCellEditor *TimeEditor::Clone() const wxGridCellEditor *NumericEditor::Clone() const
{ {
return safenew TimeEditor(mFormat, mRate); return safenew NumericEditor{ mType, mFormat, mRate };
} }
wxString TimeEditor::GetValue() const wxString NumericEditor::GetValue() const
{ {
return wxString::Format(wxT("%g"), GetTimeCtrl()->GetValue()); return wxString::Format(wxT("%g"), GetNumericTextControl()->GetValue());
} }
wxString TimeEditor::GetFormat() wxString NumericEditor::GetFormat() const
{ {
return mFormat; return mFormat;
} }
double TimeEditor::GetRate() double NumericEditor::GetRate() const
{ {
return mRate; return mRate;
} }
void TimeEditor::SetFormat(const wxString &format) void NumericEditor::SetFormat(const wxString &format)
{ {
mFormat = format; mFormat = format;
} }
void TimeEditor::SetRate(double rate) void NumericEditor::SetRate(double rate)
{ {
mRate = rate; mRate = rate;
} }
TimeRenderer::~TimeRenderer() NumericRenderer::~NumericRenderer()
{ {
} }
void TimeRenderer::Draw(wxGrid &grid, void NumericRenderer::Draw(wxGrid &grid,
wxGridCellAttr &attr, wxGridCellAttr &attr,
wxDC &dc, wxDC &dc,
const wxRect &rect, const wxRect &rect,
@@ -161,25 +164,26 @@ void TimeRenderer::Draw(wxGrid &grid,
wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
wxGridTableBase *table = grid.GetTable(); wxGridTableBase *table = grid.GetTable();
TimeEditor *te = (TimeEditor *) grid.GetCellEditor(row, col); NumericEditor *ne =
static_cast<NumericEditor *>(grid.GetCellEditor(row, col));
wxString tstr; wxString tstr;
if (te) { if (ne) {
double value; double value;
table->GetValue(row, col).ToDouble(&value); table->GetValue(row, col).ToDouble(&value);
NumericTextCtrl tt(NumericConverter::TIME, &grid, NumericTextCtrl tt(mType, &grid,
wxID_ANY, wxID_ANY,
te->GetFormat(), ne->GetFormat(),
value, value,
te->GetRate(), ne->GetRate(),
wxPoint(10000, 10000), // create offscreen wxPoint(10000, 10000), // create offscreen
wxDefaultSize, wxDefaultSize,
true); true);
tstr = tt.GetString(); tstr = tt.GetString();
te->DecRef(); ne->DecRef();
} }
dc.SetBackgroundMode(wxTRANSPARENT); dc.SetBackgroundMode(wxTRANSPARENT);
@@ -212,39 +216,40 @@ void TimeRenderer::Draw(wxGrid &grid,
grid.DrawTextRectangle(dc, tstr, rect, hAlign, vAlign); grid.DrawTextRectangle(dc, tstr, rect, hAlign, vAlign);
} }
wxSize TimeRenderer::GetBestSize(wxGrid &grid, wxSize NumericRenderer::GetBestSize(wxGrid &grid,
wxGridCellAttr & WXUNUSED(attr), wxGridCellAttr & WXUNUSED(attr),
wxDC & WXUNUSED(dc), wxDC & WXUNUSED(dc),
int row, int row,
int col) int col)
{ {
wxGridTableBase *table = grid.GetTable(); wxGridTableBase *table = grid.GetTable();
TimeEditor *te = (TimeEditor *) grid.GetCellEditor(row, col); NumericEditor *ne =
static_cast<NumericEditor *>(grid.GetCellEditor(row, col));
wxSize sz; wxSize sz;
if (te) { if (ne) {
double value; double value;
table->GetValue(row, col).ToDouble(&value); table->GetValue(row, col).ToDouble(&value);
NumericTextCtrl tt(NumericConverter::TIME, &grid, NumericTextCtrl tt(mType, &grid,
wxID_ANY, wxID_ANY,
te->GetFormat(), ne->GetFormat(),
value, value,
te->GetRate(), ne->GetRate(),
wxPoint(10000, 10000), // create offscreen wxPoint(10000, 10000), // create offscreen
wxDefaultSize, wxDefaultSize,
true); true);
sz = tt.GetSize(); sz = tt.GetSize();
te->DecRef(); ne->DecRef();
} }
return sz; return sz;
} }
// Clone is required by wxwidgets; implemented via copy constructor // Clone is required by wxwidgets; implemented via copy constructor
wxGridCellRenderer *TimeRenderer::Clone() const wxGridCellRenderer *NumericRenderer::Clone() const
{ {
return safenew TimeRenderer(); return safenew NumericRenderer{ mType };
} }
ChoiceEditor::ChoiceEditor(size_t count, const wxString choices[]) ChoiceEditor::ChoiceEditor(size_t count, const wxString choices[])
@@ -389,8 +394,14 @@ Grid::Grid(wxWindow *parent,
// RegisterDataType takes ownership of renderer and editor // RegisterDataType takes ownership of renderer and editor
RegisterDataType(GRID_VALUE_TIME, RegisterDataType(GRID_VALUE_TIME,
safenew TimeRenderer, safenew NumericRenderer{ NumericConverter::TIME },
safenew TimeEditor); safenew NumericEditor
{ NumericTextCtrl::TIME, wxT("seconds"), 44100.0 });
RegisterDataType(GRID_VALUE_FREQUENCY,
safenew NumericRenderer{ NumericConverter::FREQUENCY },
safenew NumericEditor
{ NumericTextCtrl::FREQUENCY, wxT("Hz"), 44100.0 });
RegisterDataType(GRID_VALUE_CHOICE, RegisterDataType(GRID_VALUE_CHOICE,
safenew wxGridCellStringRenderer, safenew wxGridCellStringRenderer,

View File

@@ -18,6 +18,7 @@
#include <wx/grid.h> #include <wx/grid.h>
#include <wx/string.h> #include <wx/string.h>
#include <wx/window.h> #include <wx/window.h>
#include "NumericTextCtrl.h"
#if wxUSE_ACCESSIBILITY #if wxUSE_ACCESSIBILITY
#include <wx/access.h> #include <wx/access.h>
@@ -29,21 +30,20 @@ class GridAx;
class NumericTextCtrl; class NumericTextCtrl;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// TimeEditor // NumericEditor
// //
// wxGridCellEditor for the NumericTextCtrl. // wxGridCellEditor for the NumericTextCtrl.
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#define GRID_VALUE_TIME wxT("Time") #define GRID_VALUE_TIME wxT("Time")
class TimeEditor final : public wxGridCellEditor class NumericEditor /* not final */ : public wxGridCellEditor
{ {
public: public:
TimeEditor(); NumericEditor
(NumericConverter::Type type, const wxString &format, double rate);
TimeEditor(const wxString &format, double rate); ~NumericEditor();
~TimeEditor();
// Precondition: parent != NULL // Precondition: parent != NULL
void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override; void Create(wxWindow *parent, wxWindowID id, wxEvtHandler *handler) override;
@@ -60,43 +60,46 @@ class TimeEditor final : public wxGridCellEditor
void Reset() override; void Reset() override;
wxString GetFormat(); wxString GetFormat() const;
double GetRate(); double GetRate() const;
void SetFormat(const wxString &format); void SetFormat(const wxString &format);
void SetRate(double rate); void SetRate(double rate);
wxGridCellEditor *Clone() const override; wxGridCellEditor *Clone() const override;
wxString GetValue() const override; wxString GetValue() const override;
NumericTextCtrl *GetTimeCtrl() const { return (NumericTextCtrl *)m_control; } NumericTextCtrl *GetNumericTextControl() const
{ return static_cast<NumericTextCtrl *>(m_control); }
private: private:
wxString mFormat; wxString mFormat;
double mRate; double mRate;
NumericConverter::Type mType;
double mOld; double mOld;
wxString mOldString; wxString mOldString;
wxString mValueAsString; wxString mValueAsString;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// TimeRenderer // NumericRenderer
// //
// wxGridCellRenderer for the NumericTextCtrl. // wxGridCellRenderer for the NumericTextCtrl.
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class TimeRenderer final : public wxGridCellRenderer class NumericRenderer final : public wxGridCellRenderer
{ {
public: public:
~TimeRenderer() override; NumericRenderer(NumericConverter::Type type) : mType{ type } {}
~NumericRenderer() override;
void Draw(wxGrid &grid, void Draw(wxGrid &grid,
wxGridCellAttr &attr, wxGridCellAttr &attr,
wxDC &dc, wxDC &dc,
const wxRect &rect, const wxRect &rect,
int row, int row,
int col, int col,
bool isSelected) override; bool isSelected) override;
wxSize GetBestSize(wxGrid &grid, wxSize GetBestSize(wxGrid &grid,
wxGridCellAttr &attr, wxGridCellAttr &attr,
@@ -105,6 +108,9 @@ public:
int col) override; int col) override;
wxGridCellRenderer *Clone() const override; wxGridCellRenderer *Clone() const override;
private:
NumericConverter::Type mType;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------