Clean up BaseGrid a little

This commit is contained in:
Thomas Goyne 2013-09-16 12:02:17 -07:00
parent e5afaf8a45
commit 0d50820178
2 changed files with 45 additions and 72 deletions

View File

@ -55,6 +55,7 @@
#include <libaegisub/of_type_adaptor.h> #include <libaegisub/of_type_adaptor.h>
#include <algorithm> #include <algorithm>
#include <boost/range/algorithm.hpp>
#include <cmath> #include <cmath>
#include <iterator> #include <iterator>
#include <numeric> #include <numeric>
@ -63,6 +64,7 @@
#include <wx/dcbuffer.h> #include <wx/dcbuffer.h>
#include <wx/kbdstate.h> #include <wx/kbdstate.h>
#include <wx/menu.h> #include <wx/menu.h>
#include <wx/scrolbar.h>
#include <wx/sizer.h> #include <wx/sizer.h>
enum { enum {
@ -103,7 +105,7 @@ BaseGrid::BaseGrid(wxWindow* parent, agi::Context *context, const wxSize& size,
, scrollBar(new wxScrollBar(this, GRID_SCROLLBAR, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL)) , scrollBar(new wxScrollBar(this, GRID_SCROLLBAR, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL))
, byFrame(false) , byFrame(false)
, extendRow(-1) , extendRow(-1)
, active_line(0) , active_line(nullptr)
, batch_level(0) , batch_level(0)
, batch_active_line_changed(false) , batch_active_line_changed(false)
, seek_listener(context->videoController->AddSeekListener(std::bind(&BaseGrid::Refresh, this, false, nullptr))) , seek_listener(context->videoController->AddSeekListener(std::bind(&BaseGrid::Refresh, this, false, nullptr)))
@ -208,7 +210,7 @@ void BaseGrid::OnShowColMenu(wxCommandEvent &event) {
int item = event.GetId() - MENU_SHOW_COL; int item = event.GetId() - MENU_SHOW_COL;
showCol[item] = !showCol[item]; showCol[item] = !showCol[item];
std::vector<bool> map(showCol, showCol + columns); std::vector<bool> map(std::begin(showCol), std::end(showCol));
OPT_SET("Subtitle/Grid/Column")->SetListBool(map); OPT_SET("Subtitle/Grid/Column")->SetListBool(map);
SetColumnWidths(); SetColumnWidths();
@ -216,12 +218,10 @@ void BaseGrid::OnShowColMenu(wxCommandEvent &event) {
} }
void BaseGrid::OnHighlightVisibleChange(agi::OptionValue const& opt) { void BaseGrid::OnHighlightVisibleChange(agi::OptionValue const& opt) {
if (opt.GetBool()) { if (opt.GetBool())
seek_listener.Unblock(); seek_listener.Unblock();
} else
else {
seek_listener.Block(); seek_listener.Block();
}
} }
void BaseGrid::UpdateStyle() { void BaseGrid::UpdateStyle() {
@ -252,13 +252,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() == (size_t)columns); assert(column_array.size() == boost::size(showCol));
for (int i = 0; i < columns; ++i) showCol[i] = column_array[i]; boost::copy(column_array, std::begin(showCol));
SetColumnWidths(); SetColumnWidths();
// Update
AdjustScrollbar(); AdjustScrollbar();
Refresh(); Refresh(false);
} }
void BaseGrid::ClearMaps() { void BaseGrid::ClearMaps() {
@ -290,7 +289,7 @@ void BaseGrid::UpdateMaps(bool preserve_selected_rows) {
for (auto curdiag : context->ass->Line | agi::of_type<AssDialogue>()) { for (auto curdiag : context->ass->Line | agi::of_type<AssDialogue>()) {
line_index_map[curdiag] = (int)index_line_map.size(); line_index_map[curdiag] = (int)index_line_map.size();
index_line_map.push_back(curdiag); index_line_map.push_back(curdiag);
} }
if (preserve_selected_rows) { if (preserve_selected_rows) {
Selection sel; Selection sel;
@ -325,19 +324,16 @@ void BaseGrid::UpdateMaps(bool preserve_selected_rows) {
// safe to touch the active line while processing a commit event which would // safe to touch the active line while processing a commit event which would
// cause this function to be called // cause this function to be called
AssDialogue *line = active_line; AssDialogue *line = active_line;
active_line = 0; active_line = nullptr;
// The active line may have ceased to exist; pick a new one if so // The active line may have ceased to exist; pick a new one if so
if (line_index_map.size() && line_index_map.find(line) == line_index_map.end()) { if (line_index_map.size() && line_index_map.find(line) == line_index_map.end()) {
if (active_row < (int)index_line_map.size()) { if (active_row < (int)index_line_map.size())
SetActiveLine(index_line_map[active_row]); SetActiveLine(index_line_map[active_row]);
} else if (preserve_selected_rows && !selection.empty())
else if (preserve_selected_rows && !selection.empty()) {
SetActiveLine(index_line_map[sel_rows[0]]); SetActiveLine(index_line_map[sel_rows[0]]);
} else
else {
SetActiveLine(index_line_map.back()); SetActiveLine(index_line_map.back());
}
} }
else { else {
SetActiveLine(line); SetActiveLine(line);
@ -376,23 +372,13 @@ void BaseGrid::EndBatch() {
AdjustScrollbar(); AdjustScrollbar();
} }
void BaseGrid::MakeCellVisible(int row, int col, bool center) { void BaseGrid::MakeRowVisible(int row) {
int h = GetClientSize().GetHeight(); int h = GetClientSize().GetHeight();
// Get min and max visible if (row < yPos + 1)
int minVis = yPos+1; ScrollTo(row - 1);
int maxVis = yPos+h/lineHeight-3; else if (row > yPos + h/lineHeight - 3)
ScrollTo(row - h/lineHeight + 3);
// Make visible
if (!center || row < minVis || row > maxVis) {
if (center) {
ScrollTo(row - h/lineHeight/2 + 1);
}
else {
if (row < minVis) ScrollTo(row - 1);
if (row > maxVis) ScrollTo(row - h/lineHeight + 3);
}
}
} }
void BaseGrid::SelectRow(int row, bool addToSelected, bool select) { void BaseGrid::SelectRow(int row, bool addToSelected, bool select) {
@ -437,17 +423,14 @@ wxArrayInt BaseGrid::GetSelection() const {
return res; return res;
} }
void BaseGrid::OnPaint(wxPaintEvent &) { void BaseGrid::OnPaint(wxPaintEvent &) {
// Get size and pos // Get size and pos
wxSize cs = GetClientSize(); wxSize cs = GetClientSize();
cs.SetWidth(cs.GetWidth() - scrollBar->GetSize().GetWidth()); cs.SetWidth(cs.GetWidth() - scrollBar->GetSize().GetWidth());
// Find which columns need to be repainted // Find which columns need to be repainted
bool paint_columns[11]; bool paint_columns[11] = {0};
memset(paint_columns, 0, sizeof paint_columns); 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 (size_t i = 0; i < 11; ++i) {
@ -614,8 +597,11 @@ void BaseGrid::GetRowStrings(int row, AssDialogue *line, bool *paint_columns, wx
if (paint_columns[10]) { if (paint_columns[10]) {
strings[10].clear(); strings[10].clear();
// Show overrides
if (!replace)
strings[10] = to_wx(line->Text);
// Hidden overrides // Hidden overrides
if (replace) { else {
strings[10].reserve(line->Text.get().size()); strings[10].reserve(line->Text.get().size());
size_t start = 0, pos; size_t start = 0, pos;
while ((pos = line->Text.get().find('{', start)) != std::string::npos) { while ((pos = line->Text.get().find('{', start)) != std::string::npos) {
@ -628,10 +614,6 @@ void BaseGrid::GetRowStrings(int row, AssDialogue *line, bool *paint_columns, wx
strings[10] += to_wx(line->Text.get().substr(start)); strings[10] += to_wx(line->Text.get().substr(start));
} }
// Show overrides
else
strings[10] = to_wx(line->Text);
// Cap length and set text // Cap length and set text
if (strings[10].size() > 512) if (strings[10].size() > 512)
strings[10] = strings[10].Left(512) + "..."; strings[10] = strings[10].Left(512) + "...";
@ -666,9 +648,8 @@ void BaseGrid::OnMouseEvent(wxMouseEvent &event) {
bool click = event.LeftDown(); bool click = event.LeftDown();
bool dclick = event.LeftDClick(); bool dclick = event.LeftDClick();
int row = event.GetY() / lineHeight + yPos - 1; int row = event.GetY() / lineHeight + yPos - 1;
if (holding && !click) { if (holding && !click)
row = mid(0,row,GetRows()-1); row = mid(0, row, GetRows()-1);
}
AssDialogue *dlg = GetDialogue(row); AssDialogue *dlg = GetDialogue(row);
if (!dlg) row = 0; if (!dlg) row = 0;
@ -678,7 +659,7 @@ void BaseGrid::OnMouseEvent(wxMouseEvent &event) {
if (holding) { if (holding) {
if (!event.LeftIsDown()) { if (!event.LeftIsDown()) {
if (dlg) if (dlg)
MakeCellVisible(row, 0, false); MakeRowVisible(row);
holding = false; holding = false;
ReleaseMouse(); ReleaseMouse();
} }
@ -788,7 +769,7 @@ void BaseGrid::OnContextMenu(wxContextMenuEvent &evt) {
}; };
wxMenu menu; wxMenu menu;
for (int i = 0; i < columns; ++i) for (size_t i = 0; i < boost::size(showCol); ++i)
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);
} }
@ -816,9 +797,8 @@ void BaseGrid::AdjustScrollbar() {
return; return;
} }
if (!scrollBar->IsEnabled()) { if (!scrollBar->IsEnabled())
scrollBar->Enable(true); scrollBar->Enable(true);
}
int drawPerScreen = clientSize.GetHeight() / lineHeight; int drawPerScreen = clientSize.GetHeight() / lineHeight;
int rows = GetRows(); int rows = GetRows();
@ -830,9 +810,8 @@ void BaseGrid::AdjustScrollbar() {
} }
void BaseGrid::SetColumnWidths() { void BaseGrid::SetColumnWidths() {
// Width/height
int w, h; int w, h;
GetClientSize(&w,&h); GetClientSize(&w, &h);
// DC for text extents test // DC for text extents test
wxClientDC dc(this); wxClientDC dc(this);
@ -907,7 +886,7 @@ void BaseGrid::SetColumnWidths() {
colWidth[10] = 1; colWidth[10] = 1;
// Hide columns // Hide columns
for (int i = 0; i < columns; i++) { for (size_t i = 0; i < boost::size(showCol); ++i) {
if (!showCol[i]) if (!showCol[i])
colWidth[i] = 0; colWidth[i] = 0;
} }
@ -955,17 +934,15 @@ AssDialogue *BaseGrid::GetDialogue(int n) const {
} }
int BaseGrid::GetDialogueIndex(AssDialogue *diag) const { int BaseGrid::GetDialogueIndex(AssDialogue *diag) const {
std::map<AssDialogue*,int>::const_iterator it = line_index_map.find(diag); auto it = line_index_map.find(diag);
if (it != line_index_map.end()) return it->second; return it != line_index_map.end() ? it->second : -1;
return -1;
} }
bool BaseGrid::IsDisplayed(const AssDialogue *line) const { bool BaseGrid::IsDisplayed(const AssDialogue *line) const {
if (!context->videoController->IsLoaded()) return false; if (!context->videoController->IsLoaded()) return false;
int frame = context->videoController->GetFrameN(); int frame = context->videoController->GetFrameN();
return return context->videoController->FrameAtTime(line->Start,agi::vfr::START) <= frame
context->videoController->FrameAtTime(line->Start,agi::vfr::START) <= frame && && context->videoController->FrameAtTime(line->End,agi::vfr::END) >= frame;
context->videoController->FrameAtTime(line->End,agi::vfr::END) >= frame;
} }
void BaseGrid::OnCharHook(wxKeyEvent &event) { void BaseGrid::OnCharHook(wxKeyEvent &event) {
@ -987,7 +964,7 @@ void BaseGrid::OnCharHook(wxKeyEvent &event) {
void BaseGrid::OnKeyDown(wxKeyEvent &event) { void BaseGrid::OnKeyDown(wxKeyEvent &event) {
int w,h; int w,h;
GetClientSize(&w,&h); GetClientSize(&w, &h);
int key = event.GetKeyCode(); int key = event.GetKeyCode();
bool ctrl = event.CmdDown(); bool ctrl = event.CmdDown();
@ -1052,7 +1029,7 @@ void BaseGrid::OnKeyDown(wxKeyEvent &event) {
SetSelectedSet(newsel); SetSelectedSet(newsel);
MakeCellVisible(next, 0, false); MakeRowVisible(next);
return; return;
} }
} }
@ -1078,7 +1055,7 @@ void BaseGrid::SetActiveLine(AssDialogue *new_line) {
assert(new_line == 0 || line_index_map.count(new_line)); assert(new_line == 0 || line_index_map.count(new_line));
active_line = new_line; active_line = new_line;
AnnounceActiveLineChanged(active_line); AnnounceActiveLineChanged(active_line);
MakeCellVisible(GetDialogueIndex(active_line), 0, false); MakeRowVisible(GetDialogueIndex(active_line));
Refresh(false); Refresh(false);
extendRow = GetDialogueIndex(new_line); extendRow = GetDialogueIndex(new_line);
} }

View File

@ -32,17 +32,14 @@
/// @ingroup main_ui /// @ingroup main_ui
/// ///
#pragma once #pragma once
#include <libaegisub/signal.h>
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <wx/window.h>
#include <wx/grid.h>
#include <wx/scrolbar.h>
#include <libaegisub/signal.h>
#include "selection_controller.h" #include "selection_controller.h"
@ -106,15 +103,14 @@ class BaseGrid : public wxWindow, public SubtitleSelectionController {
void ScrollTo(int y); void ScrollTo(int y);
int colWidth[16]; ///< Width in pixels of each column int colWidth[13]; ///< Width in pixels of each column
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
static const int columns = 10; ///< Total number of columns bool showCol[10]; ///< Column visibility mask
bool showCol[columns]; ///< Column visibility mask
int yPos; int yPos;
@ -156,7 +152,7 @@ public:
void UpdateStyle(); void UpdateStyle();
int GetRows() const { return index_line_map.size(); } int GetRows() const { return index_line_map.size(); }
void MakeCellVisible(int row, int col,bool center=true); void MakeRowVisible(int row);
/// @brief Get dialogue by index /// @brief Get dialogue by index
/// @param n Index to look up /// @param n Index to look up