Skip to content
Permalink
d87517a20c
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
0 contributors

Users who have contributed to this file

169 lines (146 sloc) 3.77 KB
#include "Document.h"
using namespace std;
Document :: Document(EditorDataController *edc)
: edc(edc), activePage(0)
{
}
Document :: ~Document()
{
pages.clear();
}
void Document :: BuildDocument()
{
pages.clear();
pages.push_back(shared_ptr<DocumentPage>(new DocumentPage(maxWidth, maxHeight, 0)));
int numLines = edc->GetNumLines();
for (int pNum = 0; pNum<numLines; ++pNum) {
vector<shared_ptr<string>> splitPara = SplitString(edc->GetLine(pNum));
shared_ptr<DocumentPage> currentPage = *(pages.rbegin());
if (!splitPara.size()) { splitPara.push_back(shared_ptr<string>(new string())); }
bool first = true;
while (splitPara.size()) {
if (currentPage->GetNumLines() == maxHeight) {
currentPage = shared_ptr<DocumentPage>(new DocumentPage(*currentPage));
if (!first) { currentPage->ContinueParagraph(); }
pages.push_back(currentPage);
}
int numWordsAppended = currentPage->AppendParagraph(splitPara,pNum);
if (numWordsAppended) {
splitPara.erase(splitPara.begin(),splitPara.begin()+numWordsAppended);
} else if (!first && !numWordsAppended) {
throw string("The current word" + *splitPara[0] + "is unsupported by the document structure");
}
first = false;
}
}
CorrectPageNum();
}
int Document :: GetPageNum()
{
return activePage;
}
int Document :: GetPageNumContainingLine(int lineNum)
{
int pageNum = 0;
while (lineNum >= pages[pageNum]->GetNumLines()) {
lineNum -= pages[pageNum++]->GetNumLines();
}
return pageNum;
}
shared_ptr<DocumentPage> Document :: GetPage(int pageNum)
{
return pages[pageNum];
}
shared_ptr<DocumentPage> Document :: GetActivePage()
{
shared_ptr<DocumentPage> page;
if (pages.size()) { page = pages[activePage]; }
return page;
}
int Document :: GetNumPages()
{
return pages.size();
}
int Document :: GetNumParagraphs()
{
int numParas = 0;
int lastPara = -1;
for (shared_ptr<DocumentPage> page : pages) {
int paraInPage = page->GetNumParagraphs();
for (int i = 0; i<paraInPage; ++i) {
shared_ptr<DocumentParagraph> para = page->GetParagraph(i);
if (para->GetParagraphNumber() != lastPara) {
++numParas;
lastPara = para->GetParagraphNumber();
}
}
}
return numParas;
}
void Document :: PageUp()
{
if (activePage) {
--activePage;
}
}
void Document :: PageDown()
{
if ((activePage + 1) != pages.size()) {
++activePage;
}
}
void Document :: CursorToGrid(int &row, int &col)
{
shared_ptr<DocumentPage> page = GetActivePage();
shared_ptr<DocumentParagraph> para = page->GetParagraphContainingLine(row);
col = para->TranslateCol(row,col);
row = para->GetParagraphNumber();
}
int Document :: GetMaxHeight()
{
return maxHeight;
}
void Document :: SetMaxHeight(int maxHeight)
{
this->maxHeight = maxHeight;
}
int Document :: GetMaxWidth()
{
return maxWidth;
}
void Document :: SetMaxWidth(int maxWidth)
{
this->maxWidth = maxWidth;
}
vector<shared_ptr<string>> Document :: SplitString(const string &str)
{
vector<string::const_iterator> splits;
splits.push_back(str.begin());
vector<shared_ptr<string>> words;
if (str.length() != 0) {
auto itr = str.begin();
while (itr != str.end()) {
if (*itr == ' ') {
splits.push_back(itr);
}
++itr;
}
splits.push_back(itr);
}
if (splits.size() > 1) {
auto start = splits[0];
auto end = splits[1];
words.push_back(shared_ptr<string>(new string(start,end)));
for (int i = 1; i<splits.size()-1; ++i) {
auto start = splits[i]+1;
auto end = splits[i+1];
words.push_back(shared_ptr<string>(new string(start,end)));
}
}
return words;
}
// Private Methods
void Document :: CorrectPageNum()
{
if (activePage >= pages.size()) { activePage = pages.size()-1; }
}