MainWindow::MainWindow()
{
	openAction = new QAction(tr("&Open"), this);
	saveAction = new QAction(tr("&Save"), this);
	exitAction = new QAction(tr("E&xit"), this);
	equalAction = new QAction(tr("&Equalization"), this);
	otsuAction = new QAction(tr("&otsu"), this);
	isodataAction = new QAction(tr("&Isodata"), this);
	manualAction = new QAction(tr("&Manual"), this);
	gammaAction = new QAction(tr("&Gamma"), this);
	stretchingAction = new QAction(tr("&Stretching"), this);
	sigmaAction = new QAction(tr("&Sigma"), this);
	medianAction = new QAction(tr("&Median"), this);
	lineAction = new QAction(tr("&Lines"), this);
	pixelAction = new QAction(tr("&Pixels"), this);
	gaussianAction = new QAction(tr("&Gaussian"), this);
	sobelAction = new QAction(tr("&Sobel"), this);	
	horizontalAction = new QAction(tr("&Line Intensity"), this);
	cannyAction = new QAction(tr("&Canny"),this);
	sumAction = new QAction(tr("&Add"),this);
	resAction = new QAction(tr("&Substract"),this);
	multAction = new QAction(tr("&Multiply"),this);
	divAction = new QAction(tr("&Divide"),this);
	avgAction = new QAction(tr("A&verage"),this);
	andAction = new QAction(tr("&And"),this);
	orAction = new QAction(tr("&Or"),this);
	xorAction = new QAction(tr("&Xor"),this);
	notAction = new QAction(tr("&Not"),this);
	minAction = new QAction(tr("M&in"),this);
	maxAction = new QAction(tr("M&ax"),this);
	kmeansAction = new QAction(tr("&Kmeans"),this);

	saveAction->setEnabled(false);

	connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
	connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
	connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
	connect(equalAction, SIGNAL(triggered()), this, SLOT(equalization()));
	connect(otsuAction, SIGNAL(triggered()), this, SLOT(otsuThresh()));
	connect(isodataAction, SIGNAL(triggered()), this, SLOT(isodataSlot()));
	connect(manualAction, SIGNAL(triggered()), this, SLOT(manual()));
	connect(gammaAction,SIGNAL(triggered()),this,SLOT(gamma()));
	connect(stretchingAction,SIGNAL(triggered()),this,SLOT(stretching()));
	connect(sigmaAction, SIGNAL(triggered()), this, SLOT(sigma()));
	connect(medianAction, SIGNAL(triggered()), this, SLOT(median()));
	connect(lineAction, SIGNAL(triggered()), this, SLOT(line()));
	connect(pixelAction, SIGNAL(triggered()), this, SLOT(pixel()));
	connect(gaussianAction, SIGNAL(triggered()), this, SLOT(gaussian()));
	connect(sobelAction, SIGNAL(triggered()), this, SLOT(sobelEdges()));
	connect(horizontalAction,SIGNAL(triggered()),this,SLOT(horizontal()));
	connect(cannyAction,SIGNAL(triggered()),this,SLOT(cannySlot()));
	connect(sumAction,SIGNAL(triggered()),this,SLOT(sum()));
	connect(resAction,SIGNAL(triggered()),this,SLOT(res()));
	connect(multAction,SIGNAL(triggered()),this,SLOT(mult()));
	connect(divAction,SIGNAL(triggered()),this,SLOT(div()));
	connect(avgAction,SIGNAL(triggered()),this,SLOT(avg()));
	connect(andAction,SIGNAL(triggered()),this,SLOT(andSlot()));
	connect(orAction,SIGNAL(triggered()),this,SLOT(orSlot()));
	connect(xorAction,SIGNAL(triggered()),this,SLOT(xorSlot()));
	connect(notAction,SIGNAL(triggered()),this,SLOT(notSlot()));
	connect(minAction,SIGNAL(triggered()),this,SLOT(minSlot()));
	connect(maxAction,SIGNAL(triggered()),this,SLOT(maxSlot()));
	connect(kmeansAction,SIGNAL(triggered()),this,SLOT(kmeansSlot()));
	
	fileMenu = menuBar()->addMenu(tr("&File"));
	equalizationMenu = menuBar()->addMenu(tr("&Equalization"));
	thresholdMenu = menuBar()->addMenu(tr("&Thresholding"));
	contrastMenu = menuBar()->addMenu(tr("&Contrast"));
	noiseMenu = menuBar()->addMenu(tr("&Noise"));
	edgeMenu = menuBar()->addMenu(tr("E&dge"));
	operationMenu = menuBar()->addMenu(tr("&Operation"));
	boolMenu = operationMenu->addMenu(tr("&Boolean"));
	arithMenu = operationMenu->addMenu(tr("&Arithmetic"));
	relMenu = operationMenu->addMenu(tr("&Relational"));
	segmentationMenu = menuBar()->addMenu(tr("&Segmentation"));
	
	equalizationMenu->setEnabled(false);
	thresholdMenu->setEnabled(false);
	contrastMenu->setEnabled(false);
	noiseMenu->setEnabled(false);
	edgeMenu->setEnabled(false);
	operationMenu->setEnabled(false);
	segmentationMenu->setEnabled(false);
	
	fileMenu->addAction(openAction);
	fileMenu->addAction(saveAction);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAction);

	equalizationMenu->addAction(equalAction);
	
	thresholdMenu->addAction(otsuAction);
	thresholdMenu->addAction(isodataAction);
	thresholdMenu->addAction(manualAction);
	
	contrastMenu->addAction(gammaAction);
	contrastMenu->addAction(stretchingAction);
	
	noiseMenu->addAction(sigmaAction);
	noiseMenu->addAction(medianAction);
	noiseMenu->addAction(lineAction);
	noiseMenu->addAction(pixelAction);
	noiseMenu->addAction(gaussianAction);
	
	edgeMenu->addAction(sobelAction);
	edgeMenu->addAction(horizontalAction);
	edgeMenu->addAction(cannyAction);

	boolMenu->addAction(andAction);
	boolMenu->addAction(orAction);
	boolMenu->addAction(xorAction);
	boolMenu->addAction(notAction);
	
	arithMenu->addAction(sumAction);
	arithMenu->addAction(resAction);
	arithMenu->addAction(multAction);
	arithMenu->addAction(divAction);
	arithMenu->addAction(avgAction);
	
	relMenu->addAction(minAction);
	relMenu->addAction(maxAction);
	
	segmentationMenu->addAction(kmeansAction);
	//-----

	viewer = new ImageViewer(this);
	
	QScrollArea * scrollArea = new QScrollArea;
	scrollArea->setWidget(viewer);
	scrollArea->setFixedWidth(600);
    scrollArea->setWidgetResizable(true);
    
 	boxW = new QSpinBox();
 	boxW->setEnabled(false);
 	boxW->setMaximum(65535);
 	
 	boxC = new QSpinBox();
 	boxC->setEnabled(false);
 	boxC->setMaximum(65535);
	
	histoViewer = new ImageViewer(this);
	QScrollArea * histoArea = new QScrollArea;
	histoArea->setWidget(histoViewer);
	histoArea->setFixedSize(268,278);
    histoArea->setWidgetResizable(false);
	
	QVBoxLayout * rightLayout = new QVBoxLayout;
	rightLayout->addWidget(new QLabel("Window:",this));
	rightLayout->addWidget(boxW);
	rightLayout->addWidget(new QLabel("Level:",this));
	rightLayout->addWidget(boxC);
	rightLayout->addWidget(histoArea);
	
	connect(boxW,SIGNAL(valueChanged(int)),this,SLOT(changeW(int)));
	connect(boxC,SIGNAL(valueChanged(int)),this,SLOT(changeC(int)));
	
	QWidget * rightSide = new QWidget;
	rightSide->setLayout(rightLayout);
	
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addWidget(scrollArea);
	mainLayout->addWidget(rightSide);	

	
	QWidget * centralWidget = new QWidget();
	centralWidget->setLayout(mainLayout);
	
	
	setCentralWidget(centralWidget);

	setWindowTitle(tr("DICOM Image Processor"));
    setFixedSize(QSize(900,600));
}
TextEdit::TextEdit(QWidget *parent)
  : QMainWindow(parent)
{
  setToolButtonStyle(Qt::ToolButtonFollowStyle);
  setupFileActions();
  setupEditActions();
  setupTextActions();

  {
//    QMenu *helpMenu = new QMenu(tr("Help"), this);
//    menuBar()->addMenu(helpMenu);
//    helpMenu->addAction(tr("About"), this, SLOT(about()));
//    helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
  }

  textEdit = new QTextEdit(this);
  //////////////////////////////////////////////////
  connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
          this, SLOT(currentCharFormatChanged(QTextCharFormat)));
  connect(textEdit, SIGNAL(cursorPositionChanged()),
          this, SLOT(cursorPositionChanged()));

  ///////////
  //setCentralWidget(textEdit);
  //blank=new Blank(this);
  //setCentralWidget(blank);


  //create a transparent canvas and put it on the top of textEdit
  image =new MyCanvas(900,800,this);
  textEdit->setFixedSize(QSize(800,800));
  textEdit->setBackgroundRole(QPalette::Light);   //scrollArea对象的背景色设为Dark
  image->setFixedSize(QSize(800,800));
  image->setStyleSheet(QString::fromUtf8("border:1px solid #000000;"));

  QScrollArea* scrollArea = new QScrollArea;
  scrollArea->setFixedSize(QSize(1800,900));
  //scrollArea->setWidget(image);     //将画布添加到scrollArea中
  scrollArea->setBackgroundRole(QPalette::Light);   //scrollArea对象的背景色设为Dark
  //scrollArea->setBackgroundColor(QColor::white);

//    QStackedLayout *stackedLayout = new QStackedLayout;
//    stackedLayout->addWidget(image);
//    stackedLayout->addWidget(textEdit);
//    stackedLayout->setStackingMode(QStackedLayout::StackAll);

  QHBoxLayout* hLayout=new QHBoxLayout();
  hLayout->addWidget(textEdit);
  hLayout->addWidget(image);
//  scrollArea->setLayout(stackedLayout);
  scrollArea->setLayout(hLayout);
  //scrollArea->setGeometry(QRect(50,50,800,800));



  setCentralWidget(scrollArea);    //将scrollArea加入到主窗口的中心区new QPainter(this);
  scrollArea->setAlignment(Qt::AlignHCenter);
  //after canvas handle the mouse-drag event, emit it to the edittext for farther handling
  connect(image,SIGNAL(mouseMoveSig(QMouseEvent*)),this,SLOT(onMouseMove(QMouseEvent*)));
  //connect(image,SIGNAL(mouseMoveSig(QMouseEvent*)),textEdit,SLOT(mouseMoveEvent(QMouseEvent*)));
  //connect(image,SIGNAL(mouseMoveSig(QMouseEvent*)),textEdit,SLOT(cursorPositionChanged(QMouseEvent*)));
  //connect(this,SIGNAL(mouseMoveSig(QMouseEvent*)),image,SLOT(mouseMoveSlot(QMouseEvent*)));
  //connect(textEdit,SIGNAL(mouseMoveEvent(QMouseEvent*)),image,SLOT(mouseMoveSlot(QMouseEvent*)));

  // textEdit->setFocus();
  setCurrentFileName(QString());

  fontChanged(textEdit->font());
  colorChanged(textEdit->textColor());
  alignmentChanged(textEdit->alignment());

  connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
          actionSave, SLOT(setEnabled(bool)));
  connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
          this, SLOT(setWindowModified(bool)));
  connect(textEdit->document(), SIGNAL(undoAvailable(bool)),
          actionUndo, SLOT(setEnabled(bool)));
  connect(textEdit->document(), SIGNAL(redoAvailable(bool)),
          actionRedo, SLOT(setEnabled(bool)));

  setWindowModified(textEdit->document()->isModified());
  actionSave->setEnabled(textEdit->document()->isModified());
  actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
  actionRedo->setEnabled(textEdit->document()->isRedoAvailable());

  connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo()));
  connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo()));

  actionCut->setEnabled(false);
  actionCopy->setEnabled(false);

  connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut()));
  connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy()));
  connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste()));

  connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
  connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));

#ifndef QT_NO_CLIPBOARD
  connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
#endif

  //QString initialFile = ":/data/example.html";
  QString initialFile = ":/data/test.txt";
  const QStringList args = QCoreApplication::arguments();
  if (args.count() == 2)
    initialFile = args.at(1);

  if (!load(initialFile))
    fileNew();
}