BLayoutUtils: Add GetLayoutTreeDump()

Returns a debug output string listing the basic layout properties of
the view/item hierarchy.
This commit is contained in:
Ingo Weinhold 2013-09-11 04:46:36 +02:00
parent b1c3379c7e
commit 4a8da96088
2 changed files with 131 additions and 2 deletions

View File

@ -1,16 +1,21 @@
/*
* Copyright 2006, Haiku, Inc. All rights reserved.
* Copyright 2006-2013, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _LAYOUT_UTILS_H
#define _LAYOUT_UTILS_H
#include <Alignment.h>
#include <Rect.h>
#include <Size.h>
#include <String.h>
class BLayoutItem;
class BView;
class BLayoutUtils {
public:
// static float AddSizesFloat(float a, float b);
@ -36,6 +41,17 @@ public:
BAlignment alignment);
static void AlignInFrame(BView* view, BRect frame);
static BRect MoveIntoFrame(BRect rect, BSize frameSize);
// debugging
static BString GetLayoutTreeDump(BView* view);
static BString GetLayoutTreeDump(BLayoutItem* item);
private:
static void _GetLayoutTreeDump(BView* view, int level,
BString& _output);
static void _GetLayoutTreeDump(BLayoutItem* item,
int level, bool isViewLayout,
BString& _output);
};
#endif // _LAYOUT_UTILS_H

View File

@ -1,12 +1,17 @@
/*
* Copyright 2006-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2006-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <LayoutUtils.h>
#include <typeinfo>
#include <Layout.h>
#include <View.h>
#include "ViewLayoutItem.h"
// // AddSizesFloat
// float
@ -230,3 +235,111 @@ BLayoutUtils::MoveIntoFrame(BRect rect, BSize frameSize)
return rect.OffsetToSelf(leftTop);
}
/*static*/ BString
BLayoutUtils::GetLayoutTreeDump(BView* view)
{
BString result;
_GetLayoutTreeDump(view, 0, result);
return result;
}
/*static*/ BString
BLayoutUtils::GetLayoutTreeDump(BLayoutItem* item)
{
BString result;
_GetLayoutTreeDump(item, 0, false, result);
return result;
}
/*static*/ void
BLayoutUtils::_GetLayoutTreeDump(BView* view, int level, BString& _output)
{
BString indent;
indent.SetTo(' ', level * 2);
if (view == NULL) {
_output << indent << "<null view>\n";
return;
}
BRect frame = view->Frame();
BSize min = view->MinSize();
BSize max = view->MinSize();
BSize preferred = view->PreferredSize();
_output << BString().SetToFormat(
"%sview %p (%s):\n"
"%s frame: (%f, %f, %f, %f)\n"
"%s min: (%f, %f)\n"
"%s max: (%f, %f)\n"
"%s pref: (%f, %f)\n",
indent.String(), view, typeid(*view).name(),
indent.String(), frame.left, frame.top, frame.right, frame.bottom,
indent.String(), min.width, min.height,
indent.String(), max.width, max.height,
indent.String(), preferred.width, preferred.height);
if (BLayout* layout = view->GetLayout()) {
_GetLayoutTreeDump(layout, level, true, _output);
return;
}
int32 count = view->CountChildren();
for (int32 i = 0; i < count; i++) {
_output << indent << " ---\n";
_GetLayoutTreeDump(view->ChildAt(i), level + 1, _output);
}
}
/*static*/ void
BLayoutUtils::_GetLayoutTreeDump(BLayoutItem* item, int level,
bool isViewLayout, BString& _output)
{
if (BViewLayoutItem* viewItem = dynamic_cast<BViewLayoutItem*>(item)) {
_GetLayoutTreeDump(viewItem->View(), level, _output);
return;
}
BString indent;
indent.SetTo(' ', level * 2);
if (item == NULL) {
_output << indent << "<null item>\n";
return;
}
BLayout* layout = dynamic_cast<BLayout*>(item);
BRect frame = item->Frame();
BSize min = item->MinSize();
BSize max = item->MinSize();
BSize preferred = item->PreferredSize();
if (isViewLayout) {
_output << indent << BString().SetToFormat(" [layout %p (%s)]\n",
layout, typeid(*layout).name());
} else {
_output << indent << BString().SetToFormat("item %p (%s):\n",
item, typeid(*item).name());
}
_output << BString().SetToFormat(
"%s frame: (%f, %f, %f, %f)\n"
"%s min: (%f, %f)\n"
"%s max: (%f, %f)\n"
"%s pref: (%f, %f)\n",
indent.String(), frame.left, frame.top, frame.right, frame.bottom,
indent.String(), min.width, min.height,
indent.String(), max.width, max.height,
indent.String(), preferred.width, preferred.height);
if (layout == NULL)
return;
int32 count = layout->CountItems();
for (int32 i = 0; i < count; i++) {
_output << indent << " ---\n";
_GetLayoutTreeDump(layout->ItemAt(i), level + 1, false, _output);
}
}