Back to index

unity  6.0.0
ShortcutView.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Andrea Azzarone <azzaronea@gmail.com>
00017  *              Jay Taoko <jay.taoko@canonical.com>
00018  */
00019 
00020 #include "ShortcutView.h"
00021 
00022 #include <glib/gi18n-lib.h>
00023 #include <boost/algorithm/string.hpp>
00024 #include <UnityCore/GLibWrapper.h>
00025 
00026 #include "unity-shared/LineSeparator.h"
00027 #include "unity-shared/StaticCairoText.h"
00028 #include "unity-shared/UScreen.h"
00029 
00030 namespace unity
00031 {
00032 namespace shortcut
00033 {
00034 namespace
00035 {
00036   int SECTION_NAME_FONT_SIZE = 17/1.33;
00037   int SHORTKEY_ENTRY_FONT_SIZE = 13/1.33;
00038   int INTER_SPACE_SHORTKEY_DESCRIPTION = 10;
00039   int SHORTKEY_COLUMN_WIDTH = 150;
00040   int DESCRIPTION_COLUMN_WIDTH = 265;
00041   int LINE_SPACING = 5;
00042 
00043   // We need this class because SetVisible doesn't work for layouts.
00044   class SectionView : public nux::View
00045   {
00046     public:
00047       SectionView(NUX_FILE_LINE_DECL)
00048         : nux::View(NUX_FILE_LINE_PARAM)
00049       {
00050       }
00051 
00052     protected:
00053       void Draw(nux::GraphicsEngine& graphics_engine, bool force_draw)
00054       {
00055       }
00056 
00057       void DrawContent(nux::GraphicsEngine& graphics_engine, bool force_draw)
00058       {
00059         if (GetLayout())
00060           GetLayout()->ProcessDraw(graphics_engine, force_draw);
00061       }
00062   };
00063 
00064 } // unnamed namespace
00065 
00066 NUX_IMPLEMENT_OBJECT_TYPE(View);
00067 
00068 View::View()
00069   : ui::UnityWindowView()
00070   , x_adjustment_(0)
00071   , y_adjustment_(0)
00072 {
00073   layout_ = new nux::VLayout();
00074   layout_->SetPadding(50, 38);
00075   layout_->SetSpaceBetweenChildren(20);
00076   SetLayout(layout_);
00077 
00078   std::string header = "<b>";
00079   header += _("Keyboard Shortcuts");
00080   header += "</b>";
00081 
00082   nux::StaticText* header_view = new nux::StaticText(header, NUX_TRACKER_LOCATION);
00083   header_view->SetTextPointSize(20/1.33);
00084   header_view->SetFontName("Ubuntu");
00085   layout_->AddView(header_view, 1 , nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00086 
00087   layout_->AddView(new HSeparator(), 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00088 
00089   columns_layout_ = new nux::HLayout();
00090   columns_layout_->SetSpaceBetweenChildren(30);
00091   layout_->AddLayout(columns_layout_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00092 
00093   // Column 1...
00094   columns_.push_back(new nux::VLayout());
00095   columns_layout_->AddLayout(columns_[0], 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00096 
00097   // Column 2...
00098   columns_.push_back(new nux::VLayout());
00099   columns_layout_->AddLayout(columns_[1], 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00100 }
00101 
00102 View::~View()
00103 {
00104 }
00105 
00106 void View::SetModel(Model::Ptr model)
00107 {
00108   model_ = model;
00109 
00110   // Fills the columns...
00111   RenderColumns();
00112 }
00113 
00114 Model::Ptr View::GetModel()
00115 {
00116   return model_;
00117 }
00118 
00119 void View::SetAdjustment(int x, int y)
00120 {
00121   x_adjustment_ = x;
00122   y_adjustment_ = y;
00123 }
00124 
00125 bool View::GetBaseGeometry(nux::Geometry& geo)
00126 {
00127   UScreen* uscreen = UScreen::GetDefault();
00128   int primary_monitor = uscreen->GetMonitorWithMouse();
00129   auto monitor_geo = uscreen->GetMonitorGeometry(primary_monitor);
00130 
00131   int w = GetAbsoluteWidth();
00132   int h = GetAbsoluteHeight();
00133 
00134   if (x_adjustment_ + w > monitor_geo.width ||
00135       y_adjustment_ + h > monitor_geo.height)
00136     return false;
00137 
00138   geo.width = w;
00139   geo.height = h;
00140 
00141   geo.x = monitor_geo.x + x_adjustment_ + (monitor_geo.width - geo.width -  x_adjustment_) / 2;
00142   geo.y = monitor_geo.y + y_adjustment_ + (monitor_geo.height - geo.height -  y_adjustment_) / 2;
00143   return true;
00144 }
00145 
00146 nux::LinearLayout* View::CreateSectionLayout(const char* section_name)
00147 {
00148   nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00149 
00150   std::string name("<b>");
00151   name += glib::String(g_markup_escape_text(section_name, -1)).Str();
00152   name += "</b>";
00153 
00154   nux::StaticText* section_name_view = new nux::StaticText(name, NUX_TRACKER_LOCATION);
00155   section_name_view->SetTextPointSize(SECTION_NAME_FONT_SIZE);
00156   section_name_view->SetFontName("Ubuntu");
00157   layout->AddView(new nux::SpaceLayout(10, 10, 10, 10), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
00158   layout->AddView(section_name_view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
00159   layout->AddView(new nux::SpaceLayout(15, 15, 15, 15), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
00160 
00161   return layout;
00162 }
00163 
00164 nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint)
00165 {
00166   nux::View* view = new SectionView(NUX_TRACKER_LOCATION);
00167 
00168   nux::HLayout* layout = new nux::HLayout("EntryLayout", NUX_TRACKER_LOCATION);
00169   view->SetLayout(layout);
00170 
00171   nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
00172   nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
00173 
00174   glib::String shortkey(g_markup_escape_text(hint->shortkey().c_str(), -1));
00175 
00176   std::string skey = "<b>";
00177   skey += shortkey.Str();
00178   skey += "</b>";
00179 
00180   nux::StaticText* shortkey_view = new nux::StaticText(skey, NUX_TRACKER_LOCATION);
00181   shortkey_view->SetTextAlignment(nux::StaticText::ALIGN_LEFT);
00182   shortkey_view->SetFontName("Ubuntu");
00183   shortkey_view->SetTextPointSize(SHORTKEY_ENTRY_FONT_SIZE);
00184   shortkey_view->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH);
00185   shortkey_view->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH);
00186 
00187   glib::String es_desc(g_markup_escape_text(hint->description().c_str(), -1));
00188 
00189   nux::StaticText* description_view = new nux::StaticText(es_desc.Value(), NUX_TRACKER_LOCATION);
00190   description_view->SetTextAlignment(nux::StaticText::ALIGN_LEFT);
00191   shortkey_view->SetFontName("Ubuntu");
00192   description_view->SetTextPointSize(SHORTKEY_ENTRY_FONT_SIZE);
00193   description_view->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH);
00194   description_view->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH);
00195 
00196   shortkey_layout->AddView(shortkey_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00197   shortkey_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
00198   shortkey_layout->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH);
00199   shortkey_layout->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH);
00200 
00201   description_layout->AddView(description_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00202   description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
00203   description_layout->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH);
00204   description_layout->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH);
00205 
00206   layout->AddLayout(shortkey_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00207   layout->AddLayout(description_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00208   layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION);
00209   description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
00210 
00211    auto on_shortkey_changed = [](std::string const& new_shortkey, nux::View* main_view, nux::StaticText* view) {
00212       std::string skey("<b>");
00213       skey += new_shortkey;
00214       skey += "</b>";
00215 
00216       view->SetText(skey);
00217       main_view->SetVisible(!new_shortkey.empty());
00218    };
00219 
00220   hint->shortkey.changed.connect(sigc::bind(sigc::slot<void, std::string const&, nux::View*, nux::StaticText*>(on_shortkey_changed), view, shortkey_view));
00221 
00222   return view;
00223 }
00224 
00225 nux::LinearLayout* View::CreateIntermediateLayout()
00226 {
00227   nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00228   layout->SetSpaceBetweenChildren(LINE_SPACING);
00229 
00230   return layout;
00231 }
00232 
00233 nux::Geometry View::GetBackgroundGeometry()
00234 {
00235   nux::Geometry base = GetGeometry();
00236   nux::Geometry background_geo;
00237 
00238   background_geo.width = base.width;
00239   background_geo.height = base.height;
00240   background_geo.x = (base.width - background_geo.width)/2;
00241   background_geo.y = (base.height - background_geo.height)/2;
00242 
00243   return background_geo;
00244 }
00245 
00246 void View::DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry clip)
00247 {
00248   layout_->ProcessDraw(GfxContext, force_draw);
00249 }
00250 
00251 void View::RenderColumns()
00252 {
00253   int i = 0;
00254   int column = 0;
00255 
00256   for (auto category : model_->categories())
00257   {
00258     // Three sections in the fist column...
00259     if (i > 2)
00260       column = 1;
00261 
00262     nux::LinearLayout* section_layout = CreateSectionLayout(category.c_str());
00263     nux::LinearLayout* intermediate_layout = CreateIntermediateLayout();
00264     intermediate_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
00265 
00266     for (auto hint : model_->hints()[category])
00267     {
00268       nux::View* view = CreateShortKeyEntryView(hint);
00269       view->SetVisible(!hint->shortkey().empty());
00270       intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
00271     }
00272 
00273     section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00274 
00275     if (i == 0 or i==1 or i==3 or i==4)
00276     {
00277       // Add space before the line
00278       section_layout->AddView(new nux::SpaceLayout(23, 23, 23, 23), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
00279       section_layout->AddView(new HSeparator(), 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00280       // Add space after the line
00281       section_layout->AddView(new nux::SpaceLayout(20, 20, 20, 20), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
00282     }
00283 
00284     columns_[column]->AddView(section_layout, 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
00285 
00286     i++;
00287   }
00288 }
00289 
00290 //
00291 // Introspectable methods
00292 //
00293 std::string View::GetName() const
00294 {
00295   return "ShortcutView";
00296 }
00297 
00298 } // namespace shortcut
00299 } // namespace unity