[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 5ad29f9 6/8: Generalize class wx_table_genera
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 5ad29f9 6/8: Generalize class wx_table_generator more robustly |
Date: |
Thu, 17 May 2018 19:07:10 -0400 (EDT) |
branch: master
commit 5ad29f9b030468c7848411ff894913d5df922167
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Generalize class wx_table_generator more robustly
Replaced switch-on-enum with tag dispatch. Wrote a separate ctor for
each use case. Initialized all scalar data members in ctor-initializer.
This approach preserves the readability of its predecessor in that all
initializations are grouped together for each use case. It improves
robustness by performing initialization in the ctor-initializer; that
is deemed to be worth adding about twenty extra lines.
The only data member not mentioned in a ctor-initializer is the vector,
which has its own default ctor.
It seems inoffensive enough to duplicate the first five lines of both
ctors; refactoring could of course turn those into a single line.
---
group_quote_pdf_gen_wx.cpp | 4 +--
ledger_pdf_generator_wx.cpp | 4 +--
wx_table_generator.cpp | 81 +++++++++++++++++++++++++--------------------
wx_table_generator.hpp | 20 +++++++----
4 files changed, 63 insertions(+), 46 deletions(-)
diff --git a/group_quote_pdf_gen_wx.cpp b/group_quote_pdf_gen_wx.cpp
index e94cf7e..da00dbf 100644
--- a/group_quote_pdf_gen_wx.cpp
+++ b/group_quote_pdf_gen_wx.cpp
@@ -725,11 +725,11 @@ void group_quote_pdf_generator_wx::save(std::string
const& output_filename)
}
wx_table_generator table_gen
- (vc
+ (group_quote_style_tag{}
+ ,vc
,pdf_writer.dc()
,pdf_writer.get_horz_margin()
,pdf_writer.get_page_width()
- ,e_group_quote_style
);
output_aggregate_values(pdf_writer, table_gen, &pos_y);
diff --git a/ledger_pdf_generator_wx.cpp b/ledger_pdf_generator_wx.cpp
index 4dfaab0..cb5b14e 100644
--- a/ledger_pdf_generator_wx.cpp
+++ b/ledger_pdf_generator_wx.cpp
@@ -379,11 +379,11 @@ class using_illustration_table
pdf_dc.SetFont(font);
return wx_table_generator
- (vc
+ (illustration_style_tag{}
+ ,vc
,writer.dc()
,writer.get_horz_margin()
,writer.get_page_width()
- ,e_illustration_style
);
}
};
diff --git a/wx_table_generator.cpp b/wx_table_generator.cpp
index 6858ba7..5a4880a 100644
--- a/wx_table_generator.cpp
+++ b/wx_table_generator.cpp
@@ -231,18 +231,23 @@ void increase_to_if_smaller(T& first, T second)
} // Unnamed namespace.
wx_table_generator::wx_table_generator
- (std::vector<column_parameters> const& vc
+ (group_quote_style_tag // tag not referenced
+ ,std::vector<column_parameters> const& vc
,wxDC& dc
,int left_margin
,int total_width
- ,enum_pdf_table_style style
)
- :dc_(dc)
- ,left_margin_(left_margin)
- ,total_width_(total_width)
- ,char_height_(dc_.GetCharHeight())
- ,column_margin_(dc_.GetTextExtent("M").x)
- ,max_header_lines_(1)
+ :dc_ (dc)
+ ,left_margin_ (left_margin)
+ ,total_width_ (total_width)
+ ,char_height_ (dc_.GetCharHeight())
+ // Arbitrarily use 1.333 line spacing.
+ ,row_height_ ((4 * char_height_ + 2) / 3)
+ ,column_margin_ (dc_.GetTextExtent("M").x)
+ ,max_header_lines_ (1)
+ ,draw_separators_ (true)
+ ,use_bold_headers_ (true)
+ ,align_right_ (false)
{
for(auto const& i : vc)
{
@@ -250,35 +255,41 @@ wx_table_generator::wx_table_generator
}
compute_column_widths();
- switch(style)
+ // Set a pen with zero width to make grid lines thin,
+ // and round cap style so that they combine seamlessly.
+ wxPen pen(*wxBLACK, 0);
+ pen.SetCap(wxCAP_ROUND);
+ dc_.SetPen(pen);
+}
+
+wx_table_generator::wx_table_generator
+ (illustration_style_tag // tag not referenced
+ ,std::vector<column_parameters> const& vc
+ ,wxDC& dc
+ ,int left_margin
+ ,int total_width
+ )
+ :dc_ (dc)
+ ,left_margin_ (left_margin)
+ ,total_width_ (total_width)
+ ,char_height_ (dc_.GetCharHeight())
+ ,row_height_ (char_height_)
+ ,column_margin_ (dc_.GetTextExtent("M").x)
+ ,max_header_lines_ (1)
+ ,draw_separators_ (false)
+ ,use_bold_headers_ (false)
+ // For the nonce, columns are centered by default because
+ // that's what group quotes need; this flag forces right
+ // alignment for illustrations.
+ ,align_right_ (true)
+{
+ for(auto const& i : vc)
{
- case e_illustration_style:
- {
- row_height_ = char_height_;
- draw_separators_ = false;
- use_bold_headers_ = false;
- // For the nonce, columns are centered by default because
- // that's what group quotes need; this flag forces right
- // alignment for illustrations.
- align_right_ = true;
- dc_.SetPen(illustration_rule_color);
- }
- break;
- case e_group_quote_style:
- {
- // Arbitrarily use 1.333 line spacing.
- row_height_ = (4 * char_height_ + 2) / 3;
- draw_separators_ = true;
- use_bold_headers_ = true;
- align_right_ = false;
- // Set a pen with zero width to make grid lines thin,
- // and round cap style so that they combine seamlessly.
- wxPen pen(*wxBLACK, 0);
- pen.SetCap(wxCAP_ROUND);
- dc_.SetPen(pen);
- }
- break;
+ enroll_column(i.header, i.widest_text);
}
+ compute_column_widths();
+
+ dc_.SetPen(illustration_rule_color);
}
wx_table_generator::wx_table_generator(wx_table_generator const&) = default;
diff --git a/wx_table_generator.hpp b/wx_table_generator.hpp
index ba5fffb..7e0524b 100644
--- a/wx_table_generator.hpp
+++ b/wx_table_generator.hpp
@@ -41,15 +41,13 @@ struct column_parameters
std::string widest_text;
};
-/// Specialized style used as a wx_table_generator ctor argument.
+/// Specialized styles for first wx_table_generator ctor argument.
///
/// It would be possible to derive a distinct class for each use case,
/// but style differences are not great enough to warrant that.
-enum enum_pdf_table_style
- {e_illustration_style
- ,e_group_quote_style
- };
+class group_quote_style_tag {};
+class illustration_style_tag {};
// Color of rules and borders in illustrations.
wxColor const illustration_rule_color(0x00, 0x2f, 0x6c);
@@ -72,11 +70,19 @@ class wx_table_generator
static int const rows_per_group = 5;
wx_table_generator
- (std::vector<column_parameters> const& vc
+ (group_quote_style_tag
+ ,std::vector<column_parameters> const& vc
+ ,wxDC& dc
+ ,int left_margin
+ ,int total_width
+ );
+
+ wx_table_generator
+ (illustration_style_tag
+ ,std::vector<column_parameters> const& vc
,wxDC& dc
,int left_margin
,int total_width
- ,enum_pdf_table_style style
);
wx_table_generator(wx_table_generator const&);
- [lmi-commits] [lmi] master updated (a738b9f -> d31fa0f), Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master 2fd74af 3/8: Generalize class wx_table_generator, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master 02b3f47 4/8: Generalize class wx_table_generator: pen variations, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master d31fa0f 8/8: Calculate header dimensions OAOO, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master 6d8e0ea 1/8: Move most members from derived to base class, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master 5ad29f9 6/8: Generalize class wx_table_generator more robustly,
Greg Chicares <=
- [lmi-commits] [lmi] master 8d75fd0 2/8: Eradicate unneeded class illustration_table_generator, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master 3c9cc09 5/8: Generalize class wx_table_generator: row height and boolean flags, Greg Chicares, 2018/05/17
- [lmi-commits] [lmi] master d270b21 7/8: Generally avoid regex /, $/, Greg Chicares, 2018/05/17