Clean up BaseGrid a little

Eliminate the hardcoded uses of 11 all over the place and cache the
calculation of the header widths.
This commit is contained in:
Thomas Goyne 2014-04-17 17:57:29 -07:00
parent 27c1522622
commit c93d38cc34
2 changed files with 31 additions and 43 deletions

View file

@ -53,6 +53,7 @@
#include <algorithm> #include <algorithm>
#include <boost/range/algorithm.hpp> #include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <cmath> #include <cmath>
#include <iterator> #include <iterator>
#include <numeric> #include <numeric>
@ -92,6 +93,10 @@ BaseGrid::BaseGrid(wxWindow* parent, agi::Context *context)
: wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS | wxSUNKEN_BORDER) : wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS | wxSUNKEN_BORDER)
, scrollBar(new wxScrollBar(this, GRID_SCROLLBAR, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL)) , scrollBar(new wxScrollBar(this, GRID_SCROLLBAR, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL))
, seek_listener(context->videoController->AddSeekListener(std::bind(&BaseGrid::Refresh, this, false, nullptr))) , seek_listener(context->videoController->AddSeekListener(std::bind(&BaseGrid::Refresh, this, false, nullptr)))
, headerNames({
_("#"), _("L"), _("Start"), _("End"), _("Style"), _("Actor"),
_("Effect"), _("Left"), _("Right"), _("Vert"), _("Text")
})
, context(context) , context(context)
{ {
scrollBar->SetScrollbar(0,10,100,10); scrollBar->SetScrollbar(0,10,100,10);
@ -194,14 +199,11 @@ void BaseGrid::UpdateStyle() {
font.SetPointSize(OPT_GET("Subtitle/Grid/Font Size")->GetInt()); font.SetPointSize(OPT_GET("Subtitle/Grid/Font Size")->GetInt());
font.SetWeight(wxFONTWEIGHT_NORMAL); font.SetWeight(wxFONTWEIGHT_NORMAL);
wxClientDC dc(this);
dc.SetFont(font);
// Set line height // Set line height
{ lineHeight = dc.GetTextExtent("#TWFfgGhH").GetHeight() + 4;
wxClientDC dc(this);
dc.SetFont(font);
int fw,fh;
dc.GetTextExtent("#TWFfgGhH", &fw, &fh, nullptr, nullptr, &font);
lineHeight = fh + 4;
}
// Set row brushes // Set row brushes
assert(sizeof(rowColors) / sizeof(rowColors[0]) >= COLOR_LEFT_COL); assert(sizeof(rowColors) / sizeof(rowColors[0]) >= COLOR_LEFT_COL);
@ -215,8 +217,12 @@ void BaseGrid::UpdateStyle() {
// Set column widths // Set column widths
std::vector<bool> column_array(OPT_GET("Subtitle/Grid/Column")->GetListBool()); std::vector<bool> column_array(OPT_GET("Subtitle/Grid/Column")->GetListBool());
assert(column_array.size() == boost::size(showCol)); assert(column_array.size() <= showCol.size());
boost::copy(column_array, std::begin(showCol)); boost::copy(column_array, std::begin(showCol));
for (int i : boost::irange(0, column_count))
headerWidth[i] = dc.GetTextExtent(headerNames[i]).GetWidth();
SetColumnWidths(); SetColumnWidths();
AdjustScrollbar(); AdjustScrollbar();
@ -283,7 +289,7 @@ void BaseGrid::OnPaint(wxPaintEvent &) {
for (wxRegionIterator region(GetUpdateRegion()); region; ++region) { for (wxRegionIterator region(GetUpdateRegion()); region; ++region) {
wxRect updrect = region.GetRect(); wxRect updrect = region.GetRect();
int x = 0; int x = 0;
for (size_t i = 0; i < 11; ++i) { for (int i : boost::irange(0, column_count)) {
if (updrect.x < x + colWidth[i] && updrect.x + updrect.width > x && colWidth[i]) if (updrect.x < x + colWidth[i] && updrect.x + updrect.width > x && colWidth[i])
paint_columns[i] = true; paint_columns[i] = true;
x += colWidth[i]; x += colWidth[i];
@ -326,10 +332,7 @@ void BaseGrid::DrawImage(wxDC &dc, bool paint_columns[]) {
dc.DrawLine(0, 0, w, 0); dc.DrawLine(0, 0, w, 0);
dc.SetPen(*wxTRANSPARENT_PEN); dc.SetPen(*wxTRANSPARENT_PEN);
wxString strings[] = { auto strings = headerNames;
_("#"), _("L"), _("Start"), _("End"), _("Style"), _("Actor"),
_("Effect"), _("Left"), _("Right"), _("Vert"), _("Text")
};
int override_mode = OPT_GET("Subtitle/Grid/Hide Overrides")->GetInt(); int override_mode = OPT_GET("Subtitle/Grid/Hide Overrides")->GetInt();
wxString replace_char; wxString replace_char;
@ -351,7 +354,7 @@ void BaseGrid::DrawImage(wxDC &dc, bool paint_columns[]) {
} }
// Lines // Lines
else if ((curDiag = GetDialogue(curRow))) { else if ((curDiag = GetDialogue(curRow))) {
GetRowStrings(curRow, curDiag, paint_columns, strings, !!override_mode, replace_char); GetRowStrings(curRow, curDiag, paint_columns, &strings[0], !!override_mode, replace_char);
bool inSel = !!selection.count(curDiag); bool inSel = !!selection.count(curDiag);
if (inSel && curDiag->Comment) if (inSel && curDiag->Comment)
@ -385,7 +388,7 @@ void BaseGrid::DrawImage(wxDC &dc, bool paint_columns[]) {
// Draw text // Draw text
int dx = 0; int dx = 0;
int dy = i*lineHeight; int dy = i*lineHeight;
for (int j = 0; j < 11; j++) { for (int j : boost::irange(0, column_count)) {
if (colWidth[j] == 0) continue; if (colWidth[j] == 0) continue;
if (paint_columns[j]) { if (paint_columns[j]) {
@ -624,7 +627,7 @@ void BaseGrid::OnContextMenu(wxContextMenuEvent &evt) {
}; };
wxMenu menu; wxMenu menu;
for (size_t i = 0; i < boost::size(showCol); ++i) for (int i : boost::irange(0, column_count))
menu.Append(MENU_SHOW_COL + i, strings[i], "", wxITEM_CHECK)->Check(showCol[i]); menu.Append(MENU_SHOW_COL + i, strings[i], "", wxITEM_CHECK)->Check(showCol[i]);
PopupMenu(&menu); PopupMenu(&menu);
} }
@ -740,39 +743,19 @@ void BaseGrid::SetColumnWidths() {
colWidth[10] = 1; colWidth[10] = 1;
// Hide columns // Hide columns
for (size_t i = 0; i < boost::size(showCol); ++i) { for (size_t i : boost::irange(0u, showCol.size())) {
if (!showCol[i]) if (!showCol[i])
colWidth[i] = 0; colWidth[i] = 0;
} }
wxString col_names[11] = { // Ensure every visible column is at least as big as its header plus padding
_("#"), for (size_t i : boost::irange(0u, showCol.size())) {
_("L"),
_("Start"),
_("End"),
_("Style"),
_("Actor"),
_("Effect"),
_("Left"),
_("Right"),
_("Vert"),
_("Text")
};
// Ensure every visible column is at least as big as its header
for (size_t i = 0; i < 11; ++i) {
if (colWidth[i]) if (colWidth[i])
colWidth[i] = std::max(colWidth[i], dc.GetTextExtent(col_names[i]).GetWidth()); colWidth[i] = 10 + std::max(colWidth[i], headerWidth[i]);
}
// Add padding to all non-empty columns
for (size_t i = 0; i < 10; ++i) {
if (colWidth[i])
colWidth[i] += 10;
} }
// Set size of last // Set size of last
int total = std::accumulate(colWidth, colWidth + 10, 0); int total = std::accumulate(colWidth.begin(), colWidth.end(), 0);
colWidth[10] = std::max(w - total, 0); colWidth[10] = std::max(w - total, 0);
time_cols_x = colWidth[0] + colWidth[1]; time_cols_x = colWidth[0] + colWidth[1];

View file

@ -34,6 +34,7 @@
#include <libaegisub/signal.h> #include <libaegisub/signal.h>
#include <array>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <wx/window.h> #include <wx/window.h>
@ -45,6 +46,8 @@ namespace agi {
class AssDialogue; class AssDialogue;
class BaseGrid final : public wxWindow { class BaseGrid final : public wxWindow {
static const int column_count = 11;
std::vector<agi::signal::Connection> connections; std::vector<agi::signal::Connection> connections;
int lineHeight = 1; ///< Height of a line in pixels in the current font int lineHeight = 1; ///< Height of a line in pixels in the current font
bool holding = false; ///< Is a drag selection in process? bool holding = false; ///< Is a drag selection in process?
@ -85,14 +88,16 @@ class BaseGrid final : public wxWindow {
void ScrollTo(int y); void ScrollTo(int y);
int colWidth[13]; ///< Width in pixels of each column std::array<int, column_count> colWidth; ///< Width in pixels of each column
std::array<int, column_count> headerWidth; ///< Width in pixels of each column's header
std::array<wxString, column_count> headerNames;
int time_cols_x; ///< Left edge of the times columns int time_cols_x; ///< Left edge of the times columns
int time_cols_w; ///< Width of the two times columns int time_cols_w; ///< Width of the two times columns
int text_col_x; ///< Left edge of the text column int text_col_x; ///< Left edge of the text column
int text_col_w; ///< Width of the text column int text_col_w; ///< Width of the text column
bool showCol[10]; ///< Column visibility mask std::array<bool, column_count - 1> showCol; ///< Column visibility mask (Text can't be hidden)
int yPos = 0; int yPos = 0;