11 class Layout::LinearProgram
17 LinearProgram &linprog;
21 Row(LinearProgram &, unsigned);
23 float &operator[](unsigned);
31 std::vector<float> values;
38 std::vector<Column> columns;
43 LinearProgram(unsigned);
46 Row operator[](unsigned);
49 float get_variable(unsigned);
51 unsigned find_minimal_ratio(unsigned);
52 void make_basic_column(unsigned, unsigned);
57 struct Layout::Pointers
60 unsigned Geometry::*dim;
61 Packing Slot::*packing;
62 unsigned Sides::*low_margin;
63 unsigned Sides::*high_margin;
64 unsigned Layout::*spacing;
67 Layout::Pointers Layout::pointers[2] =
69 &Geometry::x, &Geometry::w,
71 &Sides::left, &Sides::right,
74 &Geometry::y, &Geometry::h,
76 &Sides::bottom, &Sides::top,
90 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
94 void Layout::set_container(Container &c)
97 throw InvalidState("This layout is already assigned to a Container");
102 void Layout::set_margin(const Sides &m)
109 void Layout::add_widget(Widget &wdg)
112 throw InvalidState("Can't add Widgets without a Container");
114 Slot *slot = create_slot(wdg);
115 for(list<Constraint>::iterator i=slot->constraints.begin(); i!=slot->constraints.end(); ++i)
116 i->target.constraints.push_back(Constraint(complement(i->type), *slot));
117 slot->index = slots.size();
118 slots.push_back(slot);
123 void Layout::remove_widget(Widget &wdg)
125 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
126 if(&(*i)->widget==&wdg)
128 for(list<Slot *>::iterator j=slots.begin(); j!=slots.end(); ++j)
131 for(list<Constraint>::iterator k=(*j)->constraints.begin(); k!=(*j)->constraints.end(); )
134 (*j)->constraints.erase(k++);
144 for(i=slots.begin(); i!=slots.end(); ++i, ++n)
152 Layout::Slot *Layout::create_slot(Widget &wdg)
154 return new Slot(*this, wdg);
157 Layout::Slot &Layout::get_slot_for_widget(Widget &wdg)
159 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
160 if(&(*i)->widget==&wdg)
163 throw InvalidParameterValue("Widget is not in the Layout");
166 Layout::ConstraintType Layout::complement(ConstraintType type)
170 else if(type==LEFT_OF)
180 void Layout::add_constraint(Widget &src, ConstraintType type, Widget &tgt)
182 Slot &src_slot = get_slot_for_widget(src);
183 Slot &tgt_slot = get_slot_for_widget(tgt);
185 for(list<Constraint>::iterator i=src_slot.constraints.begin(); i!=src_slot.constraints.end(); ++i)
186 if(i->type==type && &i->target==&tgt_slot)
189 src_slot.constraints.push_back(Constraint(type, tgt_slot));
190 tgt_slot.constraints.push_back(Constraint(complement(type), src_slot));
195 void Layout::set_gravity(Widget &wdg, int h, int v)
197 Slot &slot = get_slot_for_widget(wdg);
199 slot.horiz_pack.gravity = h;
200 slot.vert_pack.gravity = v;
205 void Layout::set_expand(Widget &wdg, bool h, bool v)
207 Slot &slot = get_slot_for_widget(wdg);
209 slot.horiz_pack.expand = h;
210 slot.vert_pack.expand = v;
215 void Layout::update()
217 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
219 (*i)->widget.autosize();
220 (*i)->geom = (*i)->widget.get_geometry();
223 solve_constraints(HORIZONTAL);
224 solve_constraints(VERTICAL);
226 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
227 (*i)->widget.set_geometry((*i)->geom);
231 void Layout::solve_constraints(int dir)
233 Pointers &ptrs = pointers[dir&VERTICAL];
235 LinearProgram linprog(slots.size()*5+1);
236 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
238 linprog.get_object_row()[(*i)->index*5] = 0.1*((*i)->*(ptrs.packing)).gravity;
239 linprog.get_object_row()[(*i)->index*5+1] = (((*i)->*(ptrs.packing)).expand ? 5 : -1);
242 LinearProgram::Row row = linprog.add_row();
243 row[(*i)->index*5] = 1;
244 row[(*i)->index*5+2] = -1;
245 row.back() = margin.*(ptrs.low_margin);
249 LinearProgram::Row row = linprog.add_row();
250 row[(*i)->index*5] = 1;
251 row[(*i)->index*5+1] = 1;
252 row[(*i)->index*5+3] = 1;
253 row.back() = container->get_geometry().*(ptrs.dim)-margin.*(ptrs.high_margin);
257 LinearProgram::Row row = linprog.add_row();
258 row[(*i)->index*5+1] = 1;
259 row[(*i)->index*5+4] = -1;
260 row.back() = (*i)->geom.*(ptrs.dim);
263 for(list<Constraint>::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j)
265 if((j->type&1)==dir && j->type!=BELOW && j->type!=LEFT_OF)
267 LinearProgram::Row row = linprog.add_row();
269 row[(*i)->index*5] = 1;
271 row[(*i)->index*5+1] = 1;
272 if(j->type&TARGET_POS)
273 row[j->target.index*5] = -1;
274 if(j->type&TARGET_DIM)
275 row[j->target.index*5+1] = -1;
277 row.back() = this->*(ptrs.spacing);
285 for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
287 (*i)->geom.*(ptrs.pos) = linprog.get_variable((*i)->index*5);
288 (*i)->geom.*(ptrs.dim) = linprog.get_variable((*i)->index*5+1);
293 Layout::Constraint::Constraint(ConstraintType t, Slot &s):
299 Layout::Packing::Packing():
305 Layout::Slot::Slot(Layout &l, Widget &w):
310 vert_pack.gravity = 1;
311 widget.signal_autosize_changed.connect(sigc::mem_fun(this, &Slot::autosize_changed));
314 void Layout::Slot::autosize_changed()
320 Layout::LinearProgram::LinearProgram(unsigned s):
328 Layout::LinearProgram::Row Layout::LinearProgram::add_row()
330 return Row(*this, n_rows++);
333 Layout::LinearProgram::Row Layout::LinearProgram::operator[](unsigned r)
336 throw InvalidParameterValue("Row index out of range");
338 return Row(*this, r);
341 Layout::LinearProgram::Row Layout::LinearProgram::get_object_row()
343 return Row(*this, 0);
346 float Layout::LinearProgram::get_variable(unsigned i)
348 if(!solved || infeasible)
349 throw InvalidState("Not solved");
351 throw InvalidParameterValue("Variable index out of range");
353 unsigned r = columns[i].basic;
354 return columns.back().values[r];
357 bool Layout::LinearProgram::solve()
359 if(solved || infeasible)
362 // Force all columns fully into existence and relocate objective row to bottom
363 for(vector<Column>::iterator i=columns.begin(); i!=columns.end(); ++i)
365 float objective = i->values.front();
366 i->values.front() = 0.0f;
367 for(vector<float>::iterator j=i->values.begin(); j!=i->values.end(); ++j)
368 i->values.front() += *j;
369 i->values.resize(n_rows+1, 0.0f);
370 i->values.back() = objective;
373 // Create artificial variables for phase 1
374 columns.resize(n_columns+n_rows-1);
375 columns.back() = columns[n_columns-1];
376 columns[n_columns-1].values.clear();
377 for(unsigned i=1; i<n_rows; ++i)
379 Column &column = columns[n_columns+i-2];
383 // Solve the phase 1 problem
386 if(columns.back().values.front())
392 // Get rid of artificial columns and restore objective row
393 columns.erase(columns.begin()+(n_columns-1), columns.end()-1);
394 for(vector<Column>::iterator i=columns.begin(); i!=columns.end(); ++i)
397 i->values.front() = i->values.back();
398 i->values.pop_back();
408 unsigned Layout::LinearProgram::find_minimal_ratio(unsigned c)
410 float best = numeric_limits<float>::infinity();
412 /* Intentionally use n_rows since we need to ignore the relocated original
413 objective row in phase 1 */
414 for(unsigned i=1; i<n_rows; ++i)
415 if(columns[c].values[i]>0)
417 float ratio = columns.back().values[i]/columns[c].values[i];
428 void Layout::LinearProgram::make_basic_column(unsigned c, unsigned r)
430 for(unsigned i=0; i<columns.size(); ++i)
431 if(i!=c && (columns[i].basic==r || (!columns[i].basic && columns[i].values[r])))
435 columns[i].values.resize(columns.back().values.size(), 0.0f);
436 columns[i].values[columns[i].basic] = 1.0f;
437 columns[i].basic = 0;
440 float scale = columns[i].values[r]/columns[c].values[r];
442 for(unsigned j=0; j<columns[i].values.size(); ++j)
445 columns[i].values[j] = scale;
447 columns[i].values[j] -= scale*columns[c].values[j];
451 columns[c].basic = r;
452 columns[c].values.clear();
455 bool Layout::LinearProgram::pivot()
457 for(unsigned i=0; i+1<columns.size(); ++i)
458 if(!columns[i].basic && columns[i].values.front()>0)
459 if(unsigned row = find_minimal_ratio(i))
461 make_basic_column(i, row);
469 Layout::LinearProgram::Row::Row(LinearProgram &lp, unsigned i):
474 float &Layout::LinearProgram::Row::operator[](unsigned c)
476 if(c>=linprog.n_columns)
477 throw InvalidParameterValue("Column index out of range");
479 Column &column = linprog.columns[c];
480 if(column.values.size()<=index)
481 column.values.resize(index+1);
483 return column.values[index];
486 float &Layout::LinearProgram::Row::back()
488 return (*this)[linprog.n_columns-1];
492 Layout::LinearProgram::Column::Column():